dragable JPanel keeps jumping back to center when edited - java

I have a JPanel with a textfield (textPanel). I added the textPanel for some reasons to another panel (contentPanel) and that to the JFrame. I have added a MouseListener to the textPanel, so that I can drag it around. The new Position is set by the methodcall setLocation (...)
Everytime I start editing the textfield, the textpanel jumps back from its actual location to the center of the JFrame. – Why? What do I have to change?
public void run() {
area1 = new JTextArea();
area1.setText("Hello!");
GridBagLayout layout = new GridBagLayout();
GridBagConstraints cc = new GridBagConstraints();
cc.fill = GridBagConstraints.NONE;
textPanel = new JPanel(layout);
cc.anchor = GridBagConstraints.PAGE_START;
cc.gridx = 0;
textPanel.add(area1, cc);
JPanel contentPanel = new JPanel(new GridBagLayout());
cc = new GridBagConstraints();
cc.anchor = GridBagConstraints.FIRST_LINE_START;
cc.gridx = 0;
cc.gridy = 0;
cc.weightx = 1;
cc.weighty = 1;
MyMouseAdapter mh = new MyMouseAdapter();
textPanel.addMouseListener(mh);
textPanel.addMouseMotionListener(mh);
contentPanel.add(textPanel, cc);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(contentPanel);
frame.setSize(800, 600);
frame.setVisible(true);
}

Related

How to stop JTextField and JTextLabel from truncating on JPanel

I have JTextFields and JLabels added to my JPanel (from left to right) every time the JButton is pressed. However, every new JTextField and JLabelthat is added becomes smaller and smaller. How do I fix this?
Also I would like to add a JScrollPane to the JPanel but having problems doing so.
public class MyExample
{
// Field members
static JPanel panel = new JPanel();
static Integer indexer = 1;
static List<JLabel> listOfLabels = new ArrayList<JLabel>();
static List<JTextField> listOfTextFields = new ArrayList<JTextField>();
static JScrollPane scrollPane = new JScrollPane( panel );
public static void main(String[] args)
{
// Construct frame
JFrame frame = new JFrame();
frame.setLayout(new GridBagLayout());
frame.setPreferredSize(new Dimension(2000, 2000));
frame.setTitle("My Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(scrollPane);
// Frame constraints
GridBagConstraints frameConstraints = new GridBagConstraints();
// Construct button
JButton addButton = new JButton("Add");
addButton.addActionListener(new ButtonListener());
// Add button to frame
frameConstraints.gridx = 0;
frameConstraints.gridy = 0;
frame.add(addButton, frameConstraints);
// Construct panel
panel.setPreferredSize(new Dimension(1000, 1000));
panel.setLayout(new GridBagLayout());
panel.setBorder(LineBorder.createBlackLineBorder());
// Add panel to frame
frameConstraints.gridx = 0;
frameConstraints.gridy = 1;
frameConstraints.weighty = 1;
frame.add(panel, frameConstraints);
// Pack frame
frame.pack();
// Make frame visible
frame.setVisible(true);
}
static class ButtonListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent arg0)
{
// Clear panel
panel.removeAll();
// Create label and text field
JTextField jTextField = new JTextField();
jTextField.setSize(100, 200);
listOfTextFields.add(jTextField);
listOfLabels.add(new JLabel("Label " + indexer));
// Create constraints
GridBagConstraints textFieldConstraints = new GridBagConstraints();
GridBagConstraints labelConstraints = new GridBagConstraints();
// Add labels and text fields
for(int i = 0; i < indexer; i++)
{
// Text field constraints
textFieldConstraints.gridx = i;
textFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
textFieldConstraints.weightx = 0.5;
textFieldConstraints.insets = new Insets(10, 10, 10, 10);
textFieldConstraints.gridy = 1;
// Label constraints
labelConstraints.gridx = i;
labelConstraints.gridy = 0;
labelConstraints.insets = new Insets(10, 10, 10, 10);
// Add them to panel
panel.add(listOfLabels.get(i), labelConstraints);
panel.add(listOfTextFields.get(i), textFieldConstraints);
}
// Align components
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = indexer;
c.weighty = 1;
panel.add(new JLabel(), c);
// Increment indexer
indexer++;
panel.updateUI();
}
}
}
However, every new JTextField and JLabelthat is added becomes smaller and smaller. How do I fix this?
panel.setPreferredSize(new Dimension(1000, 1000));
Don't set the preferred size. If the size is fixed then as you add more components they need to shrink to fit in the allowed space.
Don't attempt to set the size of any component. Let the layout manager do its job and determine the preferred size of the panel.
When creating a JTextField the code should be something like:
//JTextField jTextField = new JTextField();
JTextField jTextField = new JTextField(10);
This will allow the text field to determine its own preferred size to dispaly about 10 characters.
panel.updateUI();
Don't use updateUI(). That is used internally by Swing when you change the LAF. When you remove/add components you should be using:
panel.revalidate();
panel.repaint();
As for the JScrollPane with panel as a viewport, you should not be adding the panel to the frame at all - setting it as the viewport (like you're doing in the JScrollPane constructor) and adding the JScrollPane is sufficient. Adding the panel itself may be the cause of your problem.
As for the shrinking problem, I am still trying to understand your layout code - your use of GridBagLayout seems a tad overcomplicated to me. Maybe you can draw a simple sketch of how you would like the layout look?

How to divide three equal parts horizontally in frame java

I have splitted frames into three parts such as horizontal part in pink background and vertical part in yellow and blue background like in the image using GridBagConstraints,enter image description here
I'm using the below code to do this,
public Main()
{
JFrame maFrame = new JFrame("The main screen"); //creating main Jframe
maFrame.setSize(1000, 700);
Container container = maFrame.getContentPane();
container.setLayout(new GridBagLayout()); //setting layout of main frame
GridBagConstraints cns = new GridBagConstraints(); //creating constraint
JPanel headPanel = new JPanel(); //creating the header panel
cns.gridx = 0;
cns.gridy = 1;
cns.weightx = 0.3;
cns.weighty = 0.7;
cns.anchor = GridBagConstraints.FIRST_LINE_START;
cns.fill = GridBagConstraints.BOTH;
maFrame.setLocationRelativeTo(null); //centering frame
headPanel.setBackground(Color.YELLOW);
container.add(headPanel, cns);
JPanel panel = new JPanel();
panel.setBackground(Color.BLUE);
cns.gridx = 1;
cns.gridy = 1;
cns.weightx = 0.7;
cns.weighty = 0.7;
cns.anchor = GridBagConstraints.PAGE_START;
cns.fill = GridBagConstraints.BOTH;
container.add(panel, cns);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.PINK);
cns.gridx = 0;
cns.gridy = 0;
cns.gridwidth = 2;
cns.weightx = 1.0;
cns.weighty = 0.3;
cns.anchor = GridBagConstraints.LAST_LINE_START;
cns.fill = GridBagConstraints.BOTH;
container.add(panel1, cns);
maFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //setting the default close operation of JFrame
maFrame.pack();
maFrame.setVisible(true); //making the frame visible
}
I want to split the pink background parts into 3 parts and yellow background into two parts. I have tried to do this. But its not working for me. I do not want to use splitpane to do this. Is it possible to achieve it using GridBagConstraints? Could you please suggest me an idea to do this? thanks in advance.
Simple way to do this using GridLayout and separate the contentPane into two row and then bottom row which is divided into two columns.
import javax.swing.*;
import java.awt.*;
class JpanelSplit {
JFrame frame;
JPanel contentPane;
JPanel pinkPanel;
JPanel yellowPanel;
JPanel bluePanel;
JPanel twoPanelContainer;
public JpanelSplit() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel(new GridLayout(2,1));
pinkPanel = new JPanel();
pinkPanel.setBackground(Color.PINK);
yellowPanel = new JPanel();
yellowPanel.setBackground(Color.YELLOW);
bluePanel = new JPanel();
bluePanel.setBackground(Color.BLUE);
twoPanelContainer = new JPanel(new GridLayout(1,2));
twoPanelContainer.add(yellowPanel);
twoPanelContainer.add(bluePanel);
contentPane.add(pinkPanel);
contentPane.add(twoPanelContainer);
frame.setContentPane(contentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new JpanelSplit();
}
}

GridBagConstraints Not Correctly Aligning JRadioButton Box

GridBagConstraints aren't working even with .weightx, .weighty, and .anchor specified.
I need the component to be set in the top left but it keeps being set in the center of the screen. Below is the method I have for positioning the component.
private void initGUI(){
getContentPane().setLayout(new GridBagLayout());
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(450, 450));
Box buttonBox = Box.createVerticalBox();
ButtonGroup buttons = new ButtonGroup();
buttons.add(okOnly);
buttons.add(okCancel);
buttons.add(yesNoCancel);
buttons.add(yesNo);
buttonBox.add(okOnly);
buttonBox.add(okCancel);
buttonBox.add(yesNoCancel);
buttonBox.add(yesNo);
buttonBox.setBorder(BorderFactory.createTitledBorder("Buttons"));
GridBagConstraints gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 0;
gridConstraints.anchor = GridBagConstraints.NORTHWEST;
gridConstraints.weightx = 1;
gridConstraints.weighty = 1;
panel.add(buttonBox, gridConstraints);
getContentPane().add(panel);
}
You're giving the GridBagLayout to the contentPane, but adding a component to it without GridBagConstraints, and then giving no layout to your panel JPanel, so that it uses the default FlowLayout, and adding components to it using GridBagConstraints, constraints that have no meaning in this situation.
Solution: obviously don't do this. Only use GridBagConstraints when adding components to a container that uses GridBagLayout.
In fact, I'd simply get rid of the panel JPanel if all you need is a collection of JRadioButtons in the upper left corner:
import java.awt.*;
import javax.swing.*;
public class Gui extends JFrame {
private JRadioButton okOnly = new JRadioButton("Ok ");
private JRadioButton okCancel = new JRadioButton("Ok Cancel");
private JRadioButton yesNoCancel = new JRadioButton("Yes No Cancel");
private JRadioButton yesNo = new JRadioButton("Yes No");
private void initGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new GridBagLayout());
// JPanel panel = new JPanel();
// panel.setPreferredSize(new Dimension(450, 450));
setPreferredSize(new Dimension(450, 450));
Box buttonBox = Box.createVerticalBox();
ButtonGroup buttons = new ButtonGroup();
buttons.add(okOnly);
buttons.add(okCancel);
buttons.add(yesNoCancel);
buttons.add(yesNo);
buttonBox.add(okOnly);
buttonBox.add(okCancel);
buttonBox.add(yesNoCancel);
buttonBox.add(yesNo);
buttonBox.setBorder(BorderFactory.createTitledBorder("Buttons"));
GridBagConstraints gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 0;
gridConstraints.anchor = GridBagConstraints.NORTHWEST;
gridConstraints.weightx = 1;
gridConstraints.weighty = 1;
// panel.add(buttonBox, gridConstraints);
add(buttonBox, gridConstraints);
// getContentPane().add(panel);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private static void createAndShowGui() {
new Gui().initGUI();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

JPanel does not appear in window unless I resize the window

I am new to GUI in java, and have been using this video to learn. When I run the program, the window is blank until I resize it.
public class GUIProgram extends JFrame
{
int screenWidth = 1000; //screenSize.width;
int screenHeight = 800; //screenSize.height;
public GUIProgram()
{
super("DATABASE");
setSize(screenWidth, screenHeight);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel p = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel(new GridBagLayout());
JButton b = new JButton("Button 1");
JButton c = new JButton("Button 2");
p.add(b);
p.add(c);
JCheckBox cb = new JCheckBox("Do you LOVE bacon?");
JCheckBox cb2 = new JCheckBox("Do you LOVE cheese?");
p2.add(cb);
p2.add(cb2);
JLabel label = new JLabel("This is a label");
JTextArea tb = new JTextArea("this is a test area");
JTextField textField = new JTextField("text field");
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(15,15,15,15);
gbc.gridx = 0;
gbc.gridy = 0;
p3.add(label, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
p3.add(tb, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
p3.add(textField, gbc);
add(p, BorderLayout.SOUTH);
add(p3, BorderLayout.CENTER);
add(p2, BorderLayout.NORTH);
}
}
Thanks in advance to anyone who can lend me some advice!
Please let me know if there is some ambiguity in what I'm asking or how I explained myself.
Call setVisible() last, immediately after pack().
Tip:
setSize(screenWidth, screenHeight); Don't set the size of top level containers. Instead layout the content & call pack().

how to display image in container with panel?

hii every one,
following is my code which displays 4 panel
one is at NORTH,....WEST , SOUTH
i want to display am image at EAST at container
how is it possible?
public class ImageProcessor extends JApplet {
JPanel panel1,panel2,panel3,panel4,panel5;
JTextField nameTxt,addTxt,phoneTxt,emailTxt;
JButton capture,download,cancle,sendEmail;
JLabel head,name,add,phone,email;
//function to align components using gridBagLayOut..
private GridBagConstraints getConstraints(int gridx, int gridy,int gridwidth, int gridheight, int anchor)
{
GridBagConstraints c =new GridBagConstraints();
c.insets = new Insets(10,10,10,10);
c.ipadx = 0;
c.ipady = 0;
c.gridx = gridx;
c.gridy = gridy;
c.gridwidth = gridwidth;
c.gridheight = gridheight;
c.anchor = anchor;
return c;
}
//ends here...
public void init() {
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
panel5 = new JPanel();
nameTxt = new JTextField(20);
addTxt = new JTextField(20);
phoneTxt = new JTextField(20);
emailTxt = new JTextField(20);
capture = new JButton("capture");
download = new JButton("download");
sendEmail = new JButton("sendEmail");
head = new JLabel("BUSINESS CARD READER");
name = new JLabel("NAME:");
add = new JLabel("ADDRESS:");
phone = new JLabel("PHONE:");
email = new JLabel("EMAIL:");
Container myPane = getContentPane();
myPane.setLayout(new BorderLayout());
panel1.setLayout(new BorderLayout());
panel2.setLayout(new GridBagLayout());
panel2.add(head,getConstraints(0,0,1,1,GridBagConstraints.CENTER));
panel3.setLayout(new FlowLayout());
panel3.add(capture);
panel3.add(download);
panel3.add(sendEmail);
panel4.setLayout(new GridBagLayout());
panel4.add(name,getConstraints(0,0,1,1,GridBagConstraints.CENTER));
panel4.add(nameTxt,getConstraints(1,0,1,1,GridBagConstraints.CENTER));
panel4.add(add,getConstraints(0,1,1,1,GridBagConstraints.CENTER));
panel4.add(addTxt,getConstraints(1,1,1,1,GridBagConstraints.CENTER));
panel4.add(phone,getConstraints(0,2,1,1,GridBagConstraints.CENTER));
panel4.add(phoneTxt,getConstraints(1,2,1,1,GridBagConstraints.CENTER));
panel4.add(email,getConstraints(0,3,1,1,GridBagConstraints.CENTER));
panel4.add(emailTxt,getConstraints(1,3,1,1,GridBagConstraints.CENTER));
panel1.add(panel2,BorderLayout.NORTH);
panel1.add(panel3,BorderLayout.SOUTH);
panel1.add(panel4,BorderLayout.WEST);
panel1.add(panel5,BorderLayout.EAST);
setSize(500,500);
myPane.add(panel1,BorderLayout.CENTER);
}
public void start(){
this.setSize(800,500);
}
}
Create a JLabel without text ("") and use setIcon to set the image to be displayed.
Here is an example.
Add the picture on a JButton or JLabel to your panel5:
JButton buttonForPicture = new JButton();
buttonForPicture.setBorder(new EmptyBorder(0, 0, 0, 0));
buttonForPicture.setOpaque(false);
buttonForPicture.setIcon(new ImageIcon(imageFilePath));
panel5.add(buttonForPicture);

Categories