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
| JPanel panelReponses = new JPanelReponses(question);
panel5.add(panelReponses);
Collection choix = panelReponses.getChoix();
...
class JPanelReponses extends JPanel {
private Collection reponses;
private AbstractButton buttons[];
public JPanelReponses(Question question) {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS);
this.reponses = question.getReponses();
this.buttons = new AbstractButton[reponses.size()];
// une seule réponse possible => JRadioButton
// plusieurs réponses possibles => JCheckBox
boolean multiple = question.getMultiple();
ButtonGroup group = multiple ? null : new ButtonGroup();
int buttonIndex = 0;
for (Iterator it = reponses.iterator(); it.hasNext();) {
Reponse reponse = (Reponse) it.next();
if (multiple) {
buttons[buttonIndex] = new JCheckBox(reponse.toString());
} else {
buttons[buttonIndex] = new JRadioButton(reponse.toString());
buttonGroup.add(buttons[buttonIndex]);
}
add(buttons[buttonIndex]);
buttonIndex++;
}
}
public Collection getChoix() {
List choix = new ArrayList();
int buttonIndex = 0;
for (Iterator it = reponses.iterator(); it.hasNext();) {
Reponse reponse = (Reponse) it.next();
if (buttons[buttonIndex].isSelected()) {
choix.add(reponse);
}
buttonIndex++;
}
return choix;
}
} |
Partager