I have a JDialog having a GridBagLayout as layout. It contains a JPanel child who also has a GridBagLayout as layout. The parent contains other fields which are disposed in two columns each (label - editor). The child contains several fields which are disposed in three columns each (label - editor - label). I'd like the first columns of both the child and the parent to be aligned together. This is how it looks right now:
Parent relevant code:
GridBagConstraints contrainte = new GridBagConstraints();
Insets insetsLabel = new Insets(0, 0, 0, 0);
Insets insetsChampsEdit = new Insets(0, 10, 0, 0);
contrainte.ipady = 10;
contrainte.gridx = 0;
contrainte.gridy = 1;
contrainte.fill = GridBagConstraints.HORIZONTAL;
contrainte.gridwidth = 1;
this.add(this.m_lblPremierDepart, contrainte);
contrainte.gridx = 1;
contrainte.insets = insetsChampsEdit;
this.add(this.m_timePicker, contrainte);
contrainte.gridx = 0;
contrainte.gridy = 2;
contrainte.insets = insetsLabel; // reset insets
contrainte.gridwidth = 3;
this.add(this.m_panneauDistribution, contrainte);
contrainte.gridy = 3;
contrainte.gridwidth = 1;
this.add(this.m_lblNbMaxVehicules, contrainte);
contrainte.gridx = 1;
contrainte.insets = insetsChampsEdit;
this.add(this.m_spinNbMaxVehicules, contrainte);
Child relevant code:
GridBagConstraints contrainte = new GridBagConstraints();
Insets insetsLabel = new Insets(0, 0, 0, 0);
Insets insetsChampsEdit = new Insets(0, 10, 0, 0);
contrainte.ipady = 10;
contrainte.gridx = 0;
contrainte.gridy = 0;
contrainte.fill = GridBagConstraints.HORIZONTAL;
this.add(this.m_lblPlusPetit, contrainte);
contrainte.gridx = 1;
contrainte.insets = insetsChampsEdit;
this.add(this.m_spinPlusPetit, contrainte);
contrainte.gridx = 2;
this.add(new JLabel("minute(s)"), contrainte);
contrainte.gridx = 0;
contrainte.gridy = 1;
contrainte.insets = insetsLabel; // reset insets
this.add(this.m_lblMoyen, contrainte);
contrainte.gridx = 1;
contrainte.insets = insetsChampsEdit;
this.add(this.m_spinMoyen, contrainte);
contrainte.gridx = 2;
this.add(new JLabel("minute(s)"), contrainte);
contrainte.gridx = 0;
contrainte.gridy = 2;
contrainte.insets = insetsLabel; // reset insets
this.add(this.m_lblPlusGrand, contrainte);
contrainte.gridx = 1;
contrainte.insets = insetsChampsEdit;
this.add(this.m_spinPlusGrand, contrainte);
contrainte.gridx = 2;
this.add(new JLabel("minute(s)"), contrainte);
Is there a way to obtain the desired behavior with GridBagLayout?
The child contains several fields which are disposed in three columns each (label - editor - label).
You will also need to make this a 2 column panel: