Bonjour,

Je souhaiterais avoir vos conseils concernant le gridBagLayout et en particulier le resize des composants à l'intérieur d'un JPanel:

Voici le code utilisé

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class MyPanel extends JPanel {
 
  private final String checkBoxLabel;
  private final JTextFieldtextField textField= new JTextField();
  private final Double value;
  private final JCheckBox checkBox;
 
  public MyPanel(String checkBoxLabel, Double value) {
    super(new GridBagLayout());
    this.checkBoxLabel = checkBoxLabel;
    this.defaultVal = defaultVal;
    checkBox = new JCheckBox(this.checkBoxLabel);
    init() ;
  }
 
  private void init() {
    if (value != null) textField.setText(this.value.toString());
    textField.setVisible(false);
    textField.setPreferredSize(new Dimension(100, 20));
    add(checkBox, new GridBagConstraints(0, 0, GridBagConstraints.RELATIVE, 1, 0.1D, 0.0D, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
    add(textField, new GridBagConstraints(1, 0, GridBagConstraints.REMAINDER, 1, 0.0D, 0.0D, GridBagConstraints.FIRST_LINE_END, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 50, 0));
 
       checkBox.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        textField.setVisible(checkBox.isSelected());
  }
 
 }
}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class AllPanel extends JPanel {
 
  public AllPanel () {
    super(new GridBagLayout());
 
    onePanel = new MyPanel("one label:", 0.d);
    twoPanel = new MyPanel("two label:", 0.d);
    threePanel = new MyPanel("three label:", 0.d);
 
    fourPanel = new MyPanel("four label:", 0.d);
    fivePanel = new MyPanel("five label:", 0.d);
    sixPanel = new MyPanel("six label:", 0.d);
    init();
}
 
  private void init() {
 
    JPanel leftPanel = new JPanel(new GridBagLayout());
    JLabel label = new JLabel("left panel");
    leftPanel.add(label, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    leftPanel.add(onePanel , new GridBagConstraints(0, 1, 1, 1, 1.0D, 0.0D, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 0, 0));
    leftPanel.add(twoPanel , new GridBagConstraints(0, 2, 1, 1, 1.0D, 0.0D, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 0, 0));
    leftPanel.add(threePanel , new GridBagConstraints(0, 3, 1, 1, 1.0D, 0.0D, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 0, 0));
 
    JPanel rightPanel = new JPanel(new GridBagLayout());
    postProcPanel.add(new JLabel("right panel"), new GridBagConstraints(0, 0, 1, 1, 0.0D, 0.0D, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
        new Insets(5, 5, 5, 5), 0, 0));
    rightPanel.add(fourPanel , new GridBagConstraints(0, 1, 1, 1, 1.0D, 0.0D, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 0, 0));
    rightPanel.add(fivePanel , new GridBagConstraints(0, 2, 1, 1, 1.0D, 0.0D, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 0, 0));
    rightPanel.add(sixPanel, new GridBagConstraints(0, 3, 1, 1, 1.0D, 0.0D, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 0, 0));
 
    add(leftPanel , new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 20, 0, 5), 0, 0));
    add(rightPanel , new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 30, 0, 5), 0, 0));
 
}
Le problème est que quand je sélectionne ou pas les check box, j'ai sans arrêt un resize de mon panel. Comment éviter cela et avoir un panel fixe que les textField soient visibles ou non ?

Merci par avance