I have 5 components in a JPanel. Everything is looking smooth with the first 4 component\'s I\'ve added. However, when I try adding a 5th component to the JPanel, the spacing be
The reason your getting this output is because, that's the way the layout manager is designed.
GridBagLayout
is a (virtual) extension of the GridLayout
. In the fact that it lays it's components out in a grid, but is far more flexible then the GridLayout
. It is the closes you will get to something like a HTML table in the included layout managers.
Lets take closer look. The following code...
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("First Name"), gbc);
gbc.gridy++;
add(new JLabel("Last Name"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
add(new JTextField(15), gbc);
gbc.gridy++;
add(new JTextField(15), gbc);
Generates
Clearly, you can see the rows and columns. Each row has the same height and each column has the same width...
Now, if we add the next label and field...
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("First Name"), gbc);
gbc.gridy++;
add(new JLabel("Last Name"), gbc);
gbc.gridy++;
add(new JLabel("What is your favorite sport:"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
add(new JTextField(15), gbc);
gbc.gridy++;
add(new JTextField(15), gbc);
gbc.gridy++;
add(new JTextField(15), gbc);
And here is your current problem...
You can see that the first column's width has increased to accommodate the new label...
Now we can fix this by using gridWidth
...
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("First Name"), gbc);
gbc.gridy++;
add(new JLabel("Last Name"), gbc);
gbc.gridy++;
gbc.gridwidth = 2;
add(new JLabel("What is your favorite sport:"), gbc);
gbc.gridwidth = 1;
gbc.gridx = 1;
gbc.gridy = 0;
add(new JTextField(15), gbc);
gbc.gridy++;
add(new JTextField(15), gbc);
gbc.gridx = 2;
gbc.gridy++;
add(new JTextField(15), gbc);
nb: The last fields gridx
position has had to be increased, other wise it would actually sit over the label!
Now, that's better, but, I don't think that's quite what you want :P
We need to adjust the first two fields to span into the next cell as well...
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("First Name"), gbc);
gbc.gridy++;
add(new JLabel("Last Name"), gbc);
gbc.gridy++;
gbc.gridwidth = 2;
add(new JLabel("What is your favorite sport:"), gbc);
gbc.gridwidth = 2;
gbc.gridx = 1;
gbc.gridy = 0;
add(new JTextField(15), gbc);
gbc.gridy++;
add(new JTextField(15), gbc);
gbc.gridx = 2;
gbc.gridy++;
add(new JTextField(15), gbc);
Finally :P - Simple as pie :D
You may want to take a closer look at How to use GridBagLayout for more details...