Java GridBadLayout positioning - java

I would be grateful for any help/suggestion regarding my problem. I attached the image of my simple program which shows that the positioning of the components seems a bit off. My question is - why the ComboBox From... as well as TextField Enter value here... start so far off of the left corner? I've given gridx=0 so it positions the component on the very edge of the window, but components start some pixels off from the edge. How can I fix it?
Also, what do I need to do/consider to remove dependency of the rows on each other? I mean how to position components anywhere I want in one row without effecting the position of other components in another row. Thank you!
Piece of Code:
JPanel container = new JPanel();
container.setLayout(new GridBagLayout());
getContentPane().add(container, BorderLayout.NORTH);
TitledBorder outputCenter;
GridBagConstraints c = new GridBagConstraints();
label = new JLabel("Choose measure system to convert");
label.setFont(new Font("Times New Roman", Font.PLAIN, 20));
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 2;
c.insets = new Insets(10, 0, 20, 0);
container.add(label, c);
fromList = new JComboBox<String>(convertFrom);
c.gridx = 0;
c.gridy = 1;
c.gridwidth =1;
c.ipadx = 20;
c.anchor = GridBagConstraints.LINE_START;
container.add(fromList, c);
toList = new JComboBox<String>(convertTo);
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 1;
c.ipadx = 20;
container.add(toList, c);
//Field where user enters the value to be converted
input = new JTextField("Enter value here...");
input.setPreferredSize(new Dimension(150,30));;
input.setEditable(true);
input.setBackground(Color.WHITE);
input.setBorder(BorderFactory.createLineBorder(Color.BLACK));
input.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
input.setText("");}});
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
c.ipady = 20;
container.add(input, c);
//The area where the output/result is shown
output = new JTextArea(10,30);
output.setEditable(false);
output.setFont(new Font("Serif", Font.BOLD, 12));
output.setBackground(Color.WHITE);
outputCenter = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Output");
outputCenter.setTitleJustification(TitledBorder.CENTER);
output.setBorder(outputCenter);
c.gridx = 1;
c.gridy = 2;
c.insets = new Insets(50,5,10,10);
c.gridwidth = 3;
container.add(output, c);
//Convert button
convert = new JButton("Convert");
c.gridx = 0;
c.gridy = 3;
c.ipadx = 50;
container.add(convert, c);
}
Output:

Remember, GridBagLayout is a "flexible grid" layout manager. It still relies on the concept of rows and columns, but each row and column has it's own size, based on the requirements of the components and the constraints applied to them.
This means, that you combo box is been aligned to the left position because of a combination of c.anchor = GridBagConstraints.LINE_START and the space requirements of the JTextField sharing the same column.
You "could" change c.anchor = GridBagConstraints.LINE_START to c.anchor = GridBagConstraints.LINE_END, while will (in your case) align the combo box to the right edge of the column, for example...
Another solution would be to use a combination of containers to reduce the overall complexity of the layout. For example, you could add both the combo boxes to their own container, managing the layout requirements for them in an isolated manner and then layout that container within the large scheme of things

Related

How to make grids different in GridBagLayout in Java?

what I want to achieve is something like this:
This is my code:
JDialog messageDialog = new JDialog();
messageDialog.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
messageDialog.setBounds(0, 0, 350, 250);
messageDialog.setLocationRelativeTo(null);
messageDialog.setVisible(true);
JPanel btnPanel = new JPanel();
JPanel clearPanel = new JPanel();
JPanel labelsPanel = new JPanel();
JPanel txtPanel = new JPanel();
JButton newMessage = new JButton("New");
JButton recievedMessages = new JButton("Recieved");
JButton sendMessages = new JButton("Sent");
JButton refreshMessages = new JButton("Refresh");
JLabel recievedMessLab = new JLabel("Messages get:");
JTextPane txtToSend = new JTextPane();
btnPanel.setLayout(new GridLayout(4, 1));
btnPanel.add(newMessage);
btnPanel.add(recievedMessages);
btnPanel.add(sendMessages);
btnPanel.add(refreshMessages);
c.gridx = 0;
c.gridy = 0;
messageDialog.add(clearPanel, c);
c.gridx = 1;
c.gridy = 0;
messageDialog.add(labelsPanel, c);
c.gridx = 0;
c.gridy = 1;
messageDialog.add(btnPanel, c);
c.gridx = 1;
c.gridy = 1;
messageDialog.add(txtPanel, c);
labelsPanel.add(recievedMessLab);
I don't know why I get some free space around all the panels and I can't figure out how to resize the grids. Oracle tutorial doesn't help too. What is the easiest way to resize this? How to rid of that free space?
You need to add weight and fill information to your GridBagConstraints so the layout manager knows which components to strech over the available space.
Try the following:
c.gridx = 0;
c.gridy = 0;
c.fill = c.NONE; // dont fill (strech)
messageDialog.add(clearPanel, c);
c.gridx = 1;
c.gridy = 0;
c.weightx = 1; // horizontal weight: 1
c.fill = c.HORIZONTAL; // fill (strech) horizontally
messageDialog.add(labelsPanel, c);
c.gridx = 0;
c.gridy = 1;
c.weightx = 0; // horizontal weight: back to 0
c.weighty = 1; // vertical weight: 1
c.fill = c.VERTICAL; // fill (strech) vertically
messageDialog.add(btnPanel, c);
c.gridx = 1;
c.gridy = 1;
c.weightx = 1; // both weights: 1
c.weighty = 1; // both weights: 1
c.fill = c.BOTH; // and fill both ways, vertically and horizontally
messageDialog.add(txtPanel, c);
Revisit the part about weightx, weighty and fill in the tutorial to get a clue how they work.
PS: txtPanel is empty and txtToSend is never used?

GridBag Layout, positioning the pieces

What I want is the label and check box at the top left corner and the three buttons on the bottom right corner.
However, it doesn't appear the the anchors are working properly.
Result:
Code:
JPanel bottomPanel = new JPanel( new GridBagLayout() );
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(0, 0, 0, 20);
c.anchor = GridBagConstraints.NORTHEAST;
bottomPanel.add(spinachLabel, c);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTHEAST;
bottomPanel.add(checkbox, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 5;
c.weightx = 0.5;
c.insets = new Insets(0, 0, 0, 5);
c.anchor = GridBagConstraints.SOUTHWEST;
bottomPanel.add(applyButton, c);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 5;
c.weightx = 0.5;
c.insets = new Insets(0, 0, 0, 5);
c.anchor = GridBagConstraints.SOUTHWEST;
bottomPanel.add(refreshButton, c);
c = new GridBagConstraints();
c.gridx = 2;
c.gridy = 5;
c.weightx = 0.5;
c.anchor = GridBagConstraints.SOUTHWEST;
bottomPanel.add(cancelButton, c);
return bottomPanel;
First, we need to clarify what GridBagConstraints.anchor does: It specifies the placement of a component within the GridBagLayout cell.
spinachLabel is being placed at gridx = 0. applyButton is also being placed at gridx = 0. Therefore, they are guaranteed to be placed in cells which are in the same column. Their respective anchor constraints can move their position within their cells, but cannot move the cells themselves.
Second, you have not set any weighty constraints. Whenever a container which uses a GridBagLayout is larger than the preferred sizes of its child components, it uses the GridBagLayout’s weight constraints to decide which cells will grow to take up that extra space. When there are no weight constraints at all, as is the case for the vertical dimension in your layout, GridBagLayout doesn’t give any of the cells that extra space, and instead centers them. That’s what you’re seeing: Since no cell has a weighty set, all the cells are vertically centered.
In summary, your components will never appear at the top and bottom, unless you set some positive weighty constraints.
This doesn’t seem like a good use of GridBagLayout. When you want to place components at the edges, BorderLayout is usually a better choice:
JCheckBox checkbox = new JCheckBox("Enable Spinach Study");
JButton applyButton = new JBUtton("Apply");
JButton refreshButton = new JBUtton("Refresh");
JButton cancelButton = new JBUtton("Cancel");
JComponent buttonPane = new JPanel(new FlowLayout(FlowLayout.TRAILING));
buttonPane.add(applyButton);
buttonPane.add(refreshButton);
buttonPane.add(cancelButton);
JPanel bottomPanel = new JPanel(new BorderLayout());
bottomPanel.add(checkbox, BorderLayout.PAGE_START);
bottomPanel.add(buttonPane, BorderLayout.PAGE_END);
(A JCheckBox should always have text, rather than being placed to the right of a label. That way, the user has a much larger mouse target, and your user interface is accessibility compatible.)

Placing JLabel in the top left corner of a GridBagLayout

I'm using GridBagLayout and I want to position my JLabel and textfield on the top left corner of my JPanel. I know this question might probably be a duplicate but I couldn't find one that could explain why my code ended up centered.
JLabel user = new JLabel(ss[0]);
JLabel av = new JLabel(ss[1]);
JTextField ufield = new JTextField("");
user.setBorder(BorderFactory.createLineBorder(Color.black));
av.setBorder(BorderFactory.createLineBorder(Color.black));
ufield.setBorder(BorderFactory.createLineBorder(Color.black));
//User Label
c.anchor = GridBagConstraints.NORTHWEST;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.insets = new Insets(2, 0, 0, 2);
p.add(user, c);
//User TextField
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 5;
c.gridheight = 1;
c.weightx = 2;
c.insets = new Insets(5, 1, 1, 5);
p.add(ufield, c);
//Available Label
c.fill = GridBagConstraints.NORTHWEST;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 1;
c.insets = new Insets(5, 1, 1, 5);
p.add(av, c);
f.repaint();
f.revalidate();
This ends up looking like this :
I want it to be near the top instead of at the center
The JLabel and textfield are in fact aligned at the top left of the JPanel, however, the JPanel is not at the top left of the JFrame (which I'm guessing is the variable f), therefore overall it appears centered rather than on the top left.
You have to control the layout of the JFrame when you add the JPanel to it. You could do it using a BorderLayout like:
f.getContentPane().setLayout(new BorderLayout());
f.add(p, BorderLayout.PAGE_START);

A component takes up only one column in a GridBagLayout

I am trying to make quite a simple layout, which would be really easy in some other layout manager. However, I need to get it working for GridBagLayout. After a snippet of code:
GridBagConstraints c = new GridBagConstraints();
JPanel smallLogoPanel = new JPanel();
smallLogoPanel.setBorder(new LineBorder(Color.red, 1));//for visualizing components' display are
smallLogoPanel.setSize(50, 50);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 2;
c.weightx = 0.01;
c.weighty = 0.1;
c.fill = GridBagConstraints.BOTH;
JLabel smallLogoLabel = new JLabel(some code to get ImageIcon);
smallLogoPanel.add(smallLogoLabel);
this.infoPanel.add(smallLogoPanel, c);
JLabel label1 = new JLabel("Label 1");
label1.setFont(new Font("Verdana", Font.BOLD, 30));
label1.setBorder(new LineBorder(Color.red, 1));
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 9;
c.gridheight = 1;
c.weightx = 0.99;//for some reason smallLogoLabel takes much more space if this weightx is any smaller
c.weighty = 0.075;
this.infoPanel.add(label1, c);
JLabel label2 = new JLabel("Label 2");
label2.setFont(new Font("Verdana", Font.BOLD, 10));
label2.setBorder(new LineBorder(Color.red, 1));
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 9;
c.gridheight = 1;
c.weightx = 0.99;
c.weighty = 0.025;
this.infoPanel.add(label2, c);
... I get such a result:
... which is absolutely fine. The problem is when I add two more labels below it:
JLabel label3 = new JLabel("Label 3");
label3.setFont(new Font("Verdana", Font.BOLD, 20));
label3.setBorder(new LineBorder(Color.red, 1));
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 5;
c.gridheight = 1;
c.weightx = 0.5;
c.weighty = 0.05;
c.anchor = GridBagConstraints.CENTER;
this.infoPanel.add(label3, c);
JLabel label4 = new JLabel("Label 4");
label4.setFont(new Font("Verdana", Font.BOLD, 20));
label4.setBorder(new LineBorder(Color.red, 1));
c.gridx = 5;
c.gridy = 2;
c.gridwidth = 5;
c.gridheight = 1;
c.weightx = 0.5;
c.weighty = 0.05;
c.insets.left = 0;
this.infoPanel.add(label4, c);
...the result looks like:
Widths of top row become different, particularly panel with icon becomes wider. Another issue - bottom row. I expected both labels to be of an equal width. However, it seems the left one takes up only 1 column which is of the same width as the panel with icon.
I would be really glad I anyone could help me with this one as I am totally stuck now.
All components in a single column of a GridBagLayout have exactly the same weightx: The maximum weightx of any GridBagConstraints belonging to any component in that column.
Initially, the weightx of column 0 was 0.01. But then you added something else in column 0, on a different row, with a weightx of 0.5, so now all components in column 0 are treated by GridBagLayout as if they have a weightx of 0.5.
The same rules apply to rows and weighty values.
By the way, it is a common misconception that gridwidth = 9 can make a cell nine times wider than it would be if its constraints were just gridwidth = 1. That is not what actually happens. A gridwidth of 9 will not be any wider, unless another row actually contains non-empty cells in the those columns. This is because GridBagLayout cell sizes are flexible, and their width is zero unless they contain components. Only weights determine relative size of cells. gridwidth and gridheight do not.

Choosing a layout manager

I'm trying to set up a couple of labels and textfields in a table-like style.
Currently I'm using the GridLayout, which kind of works as I want it to. However, the TextFields and Button is expanding to the full size of the cell.
How can I make the TextFields and Button "normal" sized, and what Layout Manager would accomplish this the easiest way?
Here's the current code and a screenshot:
JPanel forms = new JPanel();
forms.setLayout(new GridLayout(3, 2));
JLabel lbl_navn = new JLabel("Virksomhedsnavn:");
JTextField txt_navn = new JTextField();
JLabel lbl_adresse = new JLabel("Adresse:");
JTextField txt_adresse = new JTextField();
forms.add(lbl_navn);
forms.add(txt_navn);
forms.add(lbl_adresse);
forms.add(txt_adresse);
forms.add(Box.createRigidArea(new Dimension(10, 10)));
forms.add(new JButton("Opret virksomhed"));
I suggest you use gridBagLayout:
http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
As an example:
JPanel forms = new JPanel();
forms.setLayout(new GridBagLayout());
JLabel lbl_navn = new JLabel("Virksomhedsnavn:");
JTextField txt_navn = new JTextField();
JLabel lbl_adresse = new JLabel("Adresse:");
JTextField txt_adresse = new JTextField();
//Setting grid bag constraints
GridBagConstraints c = new GridBagConstraints();
// Grid position coordinates
c.gridx = 0; c.gridy = 0;
//Align panel in top-left corner
c.anchor=GridBagConstraints.LINE_START;
forms.add(lbl_navn, c);
c.gridx = 1; c.ipadx = 195;
forms.add(txt_navn, c);
c.gridy = 1; c.gridx = 0; c.ipadx = 0;
forms.add(lbl_adresse, c);
c.gridx = 1; c.ipadx = 195;
forms.add(txt_adresse,c);
c.gridy = 2; c.gridx = 0; c.ipadx = 0;
forms.add(Box.createRigidArea(new Dimension(10, 10)),c);
c.anchor=GridBagConstraints.CENTER;
c.gridy = 2; c.gridx = 1;
forms.add(new JButton("Opret virksomhed"),c);
Hope this helps.
I think you can refer to this question where useful answers have been given:
JTextField Fixed Height
I read good things about Mig layout a while ago, I never used, but seems very powerful.
Check this Mig layout, may be a good solution for your problems.
UPDATE:
Mig layout is not a "standard" layout from sun/oracle.
GridBagLayout as mentioned, can accomplished your objectives(from experience). I personally when I want fixed sizes components I use to work with "null layouts".

Categories