I don't understand something with GridBagLayout
. This is a screenshot to explain
For the JTextField "Janvier", gridy = 0, gridx = 1 and **gridwidth = 5**
For the JTextField "Février", gridy = 0, gridx = 5 and gridwidth = 4
For the week number 1, 2, 3 and 4 : gridy = 1, gridx = 0, 1, 2 and 3, and gridwidth = 1
For the week number 5 : gridy = 1, gridx = 4 and gridwidth = 2.
In fact, I would like to have week 5 under both "Janvier" and "Février" JTextField.
What do you think about this? did I forget something?
Thanks.
EDIT : sorry, i've made a mistake in the explanation. The first JTextField "Janvier" has gridx = 0.
This is a new screenshot with the code of it : enter image description here
JPanel pan = new MyGridBagPan();
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.gridheight = 1;
c.gridwidth = 5;
c.gridx = 0;
c.gridy = 0;
JTextField field = new MyLeftField("Janvier", "", 5, 14, Color.DARK_GRAY, Font.PLAIN, Color.WHITE, false);
pan.add(field, c);
c.gridwidth = 1;
c.gridx = 0;
c.gridy++;
field = new MyLeftField("1", "", 2, 14, Color.DARK_GRAY, Font.PLAIN, Color.WHITE, false);
pan.add(field, c);
c.gridx++;
field = new MyLeftField("2", "", 2, 14, Color.DARK_GRAY, Font.PLAIN, Color.WHITE, false);
pan.add(field, c);
c.gridx++;
field = new MyLeftField("3", "", 2, 14, Color.DARK_GRAY, Font.PLAIN, Color.WHITE, false);
pan.add(field, c);
c.gridx++;
field = new MyLeftField("4", "", 2, 14, Color.DARK_GRAY, Font.PLAIN, Color.WHITE, false);
pan.add(field, c);
c.gridy = 0;
c.gridx = 5;
c.gridwidth = 5;
field = new MyLeftField("Février", "", 0, 14, Color.DARK_GRAY, Font.PLAIN, Color.WHITE, false);
pan.add(field, c);
c.gridwidth = 2;
c.gridx = 4;
c.gridy = 1;
field = new MyLeftField("5", "", 2, 14, Color.DARK_GRAY, Font.PLAIN, Color.WHITE, false);
pan.add(field, c);
c.gridwidth = 1;
c.gridx+=2;
field = new MyLeftField("6", "", 2, 14, Color.DARK_GRAY, Font.PLAIN, Color.WHITE, false);
pan.add(field, c);
c.gridx++;
field = new MyLeftField("7", "", 2, 14, Color.DARK_GRAY, Font.PLAIN, Color.WHITE, false);
pan.add(field, c);
c.gridx++;
field = new MyLeftField("8", "", 2, 14, Color.DARK_GRAY, Font.PLAIN, Color.WHITE, false);
pan.add(field, c);
c.gridx++;
field = new MyLeftField("9", "", 2, 14, Color.DARK_GRAY, Font.PLAIN, Color.WHITE, false);
pan.add(field, c);
JOptionPane.showOptionDialog(null, pan
, "test", JOptionPane.DEFAULT_OPTION, JOptionPane.DEFAULT_OPTION, null, null, null);
For the week number 5 : gridy = 1, gridx = 4 and gridwidth = 2.
In your example the second row only has 7 components. Each component takes up a single column.
By default a column has a default size of 0 if no component has been added to the column, so you can't just say a component takes up two cells because the GridBagLayout doesn't know what the with of the second column should be.
If you want week 5 to take up two columns, then you really need to create a grid with 8 columns.
This can be done by adding code like the following:
GridBagLayout gbl = new GridBagLayout();
yourPanel.setLayout( gbl );
// Set up a grid with 8 columns.
// The minimum width of a column is 50 pixels
int[] columns = new int[8];
Arrays.fill(columns, 50);
gbl.columnWidths = columns;
Now the GridBagLayout understands what the column width should be for any column that doesn't have a real component.
So, the week 5 button should be under both Janvier and Fevrier.
See: Creating a board game layout using JLayeredPane for a complete working example using this basic approach.