Some how I can't get gridwidth to work in GridBagLayout in my simple SwingApp.
I have this code in Java Swing:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
class GridBagLayt extends JFrame {
GridBagLayt()
{
JButton jb1 = new JButton("1");
JButton jb2 = new JButton("2");
JButton jb3 = new JButton("3");
JButton jb4 = new JButton("4");
JButton jb5 = new JButton("5");
GridBagLayout lay = new GridBagLayout();
GridBagConstraints cons = new GridBagConstraints();
setLayout(lay);
cons.fill = GridBagConstraints.BOTH;
cons.gridheight = 1;
cons.weightx = 1;
cons.weighty = 1;
cons.gridx = 0; cons.gridy = 0;
cons.gridwidth = 2;
lay.setConstraints(jb1, cons);
add(jb1);
cons.gridx = 2;
lay.setConstraints(jb2, cons);
add(jb2);
cons.gridx = 0; cons.gridy = 1;
cons.gridwidth = 1;
lay.setConstraints(jb3, cons);
add(jb3);
cons.gridx = 1;
cons.gridwidth = 2;
lay.setConstraints(jb4, cons);
add(jb4);
cons.gridx = 3;
cons.gridwidth = 1;
lay.setConstraints(jb5, cons);
add(jb5);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(700,200);
setVisible(true);
}
}
public class SwingApp {
public static void main(String[] args) {
new GridBagLayt();
}
}
But instead I get this:
I really don't know what the problem is, the coordinate and gridwidth seems ok to me, and I expected this to work, but it's not. And if I add four more buttons between this two rows, then the third row seems OK, but I don't want that:
Example: If I add this code
cons.gridx = 0; cons.gridy = 0;
cons.gridwidth = 2;
lay.setConstraints(jb1, cons);
add(jb1);
cons.gridx = 2;
lay.setConstraints(jb2, cons);
add(jb2);
cons.gridx = 0; cons.gridy = 1;
cons.gridwidth = 1;
lay.setConstraints(jb3, cons);
add(jb3);
cons.gridx = 1;
lay.setConstraints(jb4, cons);
add(jb4);
cons.gridx = 2;
lay.setConstraints(jb5, cons);
add(jb5);
cons.gridx = 3;
lay.setConstraints(jb6, cons);
add(jb6);
cons.gridx = 0; cons.gridy = 2;
lay.setConstraints(jb7, cons);
add(jb7);
cons.gridx = 1; cons.gridy = 2;
cons.gridwidth = 2;
lay.setConstraints(jb8, cons);
add(jb8);
cons.gridx = 3; cons.gridy = 2;
cons.gridwidth = 1;
lay.setConstraints(jb9, cons);
add(jb9);
Then I got this:
But I don't want this second row





Any suitable complex UI/layout should be split into multiple components. This is commonly known as "compound layouts".
This allows you to better focus on the individual needs of each "area" of the layout independently.
Sometimes, you just need to fake it.
Please note, I've used labels instead of button as a demonstration only, as MacOs buttons have a lot of space around them and this better illustrates the use of the
GridBagConstraints#insetsNow, I don't know about you, but I was tempted to create some kind of factory method which would create each row, based on the number of elements I needed in each row ... but that's me
Then change the columns
weightxproperty accordingly, for example...Now, remember, the components own preferred size plays into this. Without the additional
ipadx, all three elements can be layed out their preferred size with in the available container area.And, no, you don't "need" to specify the
weightxfor each column, this is just a demonstration, but sometimes, you might find it more useful to be absolute in your desires.