bonjour
J'ai créer 10 ToggleButton. Je voudrais que l'utilisateur ne puisse en selectionner que 2 au maximun ...
Je sais qu'il existe les ButtonGroup, ais cela ne permet que dans selectionner 1 seul.
Aurriez vous une idée de comment faire ?
merci
Version imprimable
bonjour
J'ai créer 10 ToggleButton. Je voudrais que l'utilisateur ne puisse en selectionner que 2 au maximun ...
Je sais qu'il existe les ButtonGroup, ais cela ne permet que dans selectionner 1 seul.
Aurriez vous une idée de comment faire ?
merci
bon et bien a force de chercher ... j'ai trouver !!!
je vous donne mon code, merci quand meme !
c'est long mais ça peut interesser du monde ! :bug:Code:
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207 /** The panel returned*/ private VPanel panel = new VPanel(); /**The button to listen the conference 1. */ private VToggleButton conf_1 = new VToggleButton(); /**The button to listen the conference 2. */ private VToggleButton conf_2 = new VToggleButton(); /**The button to listen the conference 3. */ private VToggleButton conf_3 = new VToggleButton(); /**The button to listen the conference 4. */ private VToggleButton conf_4 = new VToggleButton(); /**The button to listen the conference 5. */ private VToggleButton conf_5 = new VToggleButton(); /**The button to listen the conference 6. */ private VToggleButton conf_6 = new VToggleButton(); /**The button to listen the conference 7. */ private VToggleButton conf_7 = new VToggleButton(); /**The button to listen the conference 8. */ private VToggleButton conf_8 = new VToggleButton(); /**The button to listen the conference 9. */ private VToggleButton conf_9 = new VToggleButton(); /**The button to listen the conference 10. */ private VToggleButton conf_10 = new VToggleButton(); /** List which contain the position of the button (pressed or not). */ private Map<VToggleButton,Boolean> buttonPosition = new HashMap<VToggleButton,Boolean>(); public ConferenceVE(String arg0) { super(arg0); buttonPosition.put(conf_1,false); buttonPosition.put(conf_2,false); buttonPosition.put(conf_3,false); buttonPosition.put(conf_4,false); buttonPosition.put(conf_5,false); buttonPosition.put(conf_6,false); buttonPosition.put(conf_7,false); buttonPosition.put(conf_8,false); buttonPosition.put(conf_9,false); buttonPosition.put(conf_10,false); } @Override public JComponent createJComponent() { panel = new VPanel(new GridBagLayout()); conf_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if(conf_1.isSelected()) { buttonPosition.put(conf_1, true); verifNombreSelec(); } else { buttonPosition.put(conf_1, false); verifNombreSelec(); } } }); conf_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if(conf_2.isSelected()) { buttonPosition.put(conf_2, true); verifNombreSelec(); } else { buttonPosition.put(conf_2, false); verifNombreSelec(); } } }); conf_3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if(conf_3.isSelected()) { buttonPosition.put(conf_3, true); verifNombreSelec(); } else { buttonPosition.put(conf_3, false); verifNombreSelec(); } } }); conf_4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if(conf_4.isSelected()) { buttonPosition.put(conf_4, true); verifNombreSelec(); } else { buttonPosition.put(conf_4, false); verifNombreSelec(); } } }); conf_5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if(conf_5.isSelected()) { buttonPosition.put(conf_5, true); verifNombreSelec(); } else { buttonPosition.put(conf_5, false); verifNombreSelec(); } } }); conf_6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if(conf_6.isSelected()) { buttonPosition.put(conf_6, true); verifNombreSelec(); } else { buttonPosition.put(conf_6, false); verifNombreSelec(); } } }); conf_7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if(conf_7.isSelected()) { buttonPosition.put(conf_7, true); verifNombreSelec(); } else { buttonPosition.put(conf_7, false); verifNombreSelec(); } } }); conf_8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if(conf_8.isSelected()) { buttonPosition.put(conf_8, true); verifNombreSelec(); } else { buttonPosition.put(conf_8, false); verifNombreSelec(); } } }); conf_9.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if(conf_9.isSelected()) { buttonPosition.put(conf_9, true); verifNombreSelec(); } else { buttonPosition.put(conf_9, false); verifNombreSelec(); } } }); conf_10.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if(conf_10.isSelected()) { buttonPosition.put(conf_10, true); verifNombreSelec(); } else { buttonPosition.put(conf_10, false); verifNombreSelec(); } } }); return panel; } /** Construction of the panel on the event aplication ready in order to have all parameters needed. */ public void onApplicationReady() { setTextButton(conf_1,"Conférence 1"); setTextButton(conf_2,"Conférence 2"); setTextButton(conf_3,"Conférence 3"); setTextButton(conf_4,"Conférence 4"); setTextButton(conf_5,"Conférence 5"); setTextButton(conf_6,"Conférence 6"); setTextButton(conf_7,"Conférence 7"); setTextButton(conf_8,"Conférence 8"); setTextButton(conf_9,"Conférence 9"); setTextButton(conf_10,"Conférence 10"); panel.add(conf_1, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,new Insets(10, 10, 10, 10), 0, 0)); panel.add(conf_2, new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,new Insets(10, 10, 10, 10), 0, 0)); panel.add(conf_3, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,new Insets(10, 10, 10, 10), 0, 0)); panel.add(conf_4, new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,new Insets(10, 10, 10, 10), 0, 0)); panel.add(conf_5, new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,new Insets(10, 10, 10, 10), 0, 0)); panel.add(conf_6, new GridBagConstraints(1, 2, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,new Insets(10, 10, 10, 10), 0, 0)); panel.add(conf_7, new GridBagConstraints(0, 3, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,new Insets(10, 10, 10, 10), 0, 0)); panel.add(conf_8, new GridBagConstraints(1, 3, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,new Insets(10, 10, 10, 10), 0, 0)); panel.add(conf_9, new GridBagConstraints(0, 4, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,new Insets(10, 10, 10, 10), 0, 0)); panel.add(conf_10, new GridBagConstraints(1, 4, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,new Insets(10, 10, 10, 10), 0, 0)); } /** Funtion to set the text to the button. */ public void setTextButton (VToggleButton button, String text) { VLabel labelText = new VLabel(); Font dialog = new Font("Dialog", Font.ROMAN_BASELINE, 32); labelText.setFont(dialog); labelText.setText(text); labelText.setHorizontalAlignment(SwingConstants.CENTER); button.add(labelText); button.setLayout(new CardLayout()); } public void actionPerformed(ActionEvent arg0) { } public void verifNombreSelec() { int k = 0; Set cles = buttonPosition.keySet(); Iterator iter = cles.iterator(); while(iter.hasNext()) { Object cle = iter.next(); if(buttonPosition.get(cle) == true) {k=k+1;} } if(k == 2){ Set cles2 = buttonPosition.keySet(); Iterator iter2 = cles2.iterator(); while(iter2.hasNext()) { Object cle2 = iter2.next(); if(buttonPosition.get(conf_1)==true) { conf_1.setEnabled(true); } else {conf_1.setEnabled(false);} if(buttonPosition.get(conf_2)==true) { conf_2.setEnabled(true); } else {conf_2.setEnabled(false);} if(buttonPosition.get(conf_3)==true) { conf_3.setEnabled(true); } else {conf_3.setEnabled(false);} if(buttonPosition.get(conf_4)==true) { conf_4.setEnabled(true); } else {conf_4.setEnabled(false);} if(buttonPosition.get(conf_5)==true) { conf_5.setEnabled(true); } else {conf_5.setEnabled(false);} if(buttonPosition.get(conf_6)==true) { conf_6.setEnabled(true); } else {conf_6.setEnabled(false);} if(buttonPosition.get(conf_7)==true) { conf_7.setEnabled(true); } else {conf_7.setEnabled(false);} if(buttonPosition.get(conf_8)==true) { conf_8.setEnabled(true); } else {conf_8.setEnabled(false);} if(buttonPosition.get(conf_9)==true) { conf_9.setEnabled(true); } else {conf_9.setEnabled(false);} if(buttonPosition.get(conf_10)==true) { conf_10.setEnabled(true); } else {conf_10.setEnabled(false);} } }else { conf_1.setEnabled(true); conf_2.setEnabled(true); conf_3.setEnabled(true); conf_4.setEnabled(true); conf_5.setEnabled(true); conf_6.setEnabled(true); conf_7.setEnabled(true); conf_8.setEnabled(true); conf_9.setEnabled(true); conf_10.setEnabled(true); } }