Bonjour,

J'ai commencé à utiliser le gridBagLayout aujourd'hui et je rencontre quelques soucis. En effet je cherche lors du clique sur un bouton "plus" ajouter au fur et à mesure des JTextfield à mon JPanel.
Cela fonctionne bien cependant la position de mon bouton est modifié ce qui ne me convient pas trop. Le GridBagConstraints.gridy du bouton à pour valeur une variable (incrémenté au fur et à mesure des ajouts de JTextfield) mais malheureusement ça ne fonctionne pas même avec un repaint ou revalidate. La coordonnée en y du bouton reste la même.

Voici mon code actuel :

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
public class PanelInfoTypeBlock extends JPanel implements ActionListener {
 
	JButton plus;
	JButton valider;
	int row = 3;
 
	ArrayList<JTextField> listeTextField;
	GridBagConstraints gbcPlus;
 
	public PanelInfoTypeBlock() {
		this.setLayout(new GridBagLayout());
 
		listeTextField = new ArrayList<>();
 
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=0;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.insets = new Insets(10, 10, 10, 10);
		this.add(new JLabel("Entrez les types de blocks : "), gbc);
 
		GridBagConstraints gbc2 = new GridBagConstraints();
		gbc2.gridx=0;
		gbc2.gridy=1;
		gbc2.weightx = 1;
		gbc2.weighty = 1;
 
		JTextField champ1 = new JTextField("");
		champ1.setPreferredSize(new Dimension(200, 20));
		champ1.setMinimumSize(new Dimension(200, 20));
		this.add(champ1, gbc2);
		listeTextField.add(champ1);
 
		gbcPlus = new GridBagConstraints();
		gbcPlus.gridx=3;
		gbcPlus.weightx = 1;
		gbcPlus.weighty = 1;
		gbcPlus.gridy=1;
 
		plus = new JButton("+");
		this.add(plus, gbcPlus);
		plus.addActionListener(this);
 
		/*valider = new JButton("Valider");
		valider.addActionListener(this);
		this.add(valider);*/
 
 
	}
 
	@Override
	public void actionPerformed(ActionEvent e) {
		JFrame frame =  (JFrame) this.getTopLevelAncestor();
 
		if(e.getSource() == plus) {
			ajoutTextField(row);
			gbcPlus.gridy=row+1;
			System.out.println(gbcPlus.gridy);
			plus.repaint();
			plus.revalidate();
			this.revalidate();
			for(int i=0; i<listeTextField.size(); i++) {
				System.out.println(listeTextField.get(i).getText());
			}
		}
		else if(e.getSource() == valider) {
			 Dimension dimension = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
	           	frame.setSize((int)dimension.getWidth(), (int)dimension.getHeight()-30);
	   			frame.setLocationRelativeTo(null);
	   			frame.getContentPane().removeAll();
	   			frame.setContentPane(new PanelCreation());
	   			frame.repaint();
	   			frame.validate();
		}
 
	}
 
	public void ajoutTextField(int row) {
		GridBagConstraints gbc3 = new GridBagConstraints();
		gbc3.gridx=0;
		gbc3.gridy=row;
		gbc3.weightx = 1;
		gbc3.weighty = 1;
		this.row++;
		JTextField champ1 = new JTextField("");
		champ1.setPreferredSize(new Dimension(200, 20));
		champ1.setMinimumSize(new Dimension(200, 20));
		this.add(champ1, gbc3);
		listeTextField.add(champ1);
	}
}
Est ce que mon idée est bonne ou pas du tout ?