Choix multiple
Fermé
isa.dama
-
Modifié le 21 sept. 2020 à 10:47
KX Messages postés 16668 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 17 mars 2023 - 21 sept. 2020 à 19:51
KX Messages postés 16668 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 17 mars 2023 - 21 sept. 2020 à 19:51
1 réponse
KX
Messages postés
16668
Date d'inscription
samedi 31 mai 2008
Statut
Modérateur
Dernière intervention
17 mars 2023
3 005
21 sept. 2020 à 19:51
21 sept. 2020 à 19:51
Bonjour,
Tout d'abord, très mauvaise idée d'apprendre à faire du Swing avec NetBeans, cela génère du code crade, difficile à comprendre ou à modifier.
Quant à ton besoin, il peut se faire grâce à une JComboBox<JCheckBox>
Exemple :

Tout d'abord, très mauvaise idée d'apprendre à faire du Swing avec NetBeans, cela génère du code crade, difficile à comprendre ou à modifier.
Quant à ton besoin, il peut se faire grâce à une JComboBox<JCheckBox>
Exemple :

import java.util.function.LongFunction;
import javax.swing.*;
public class JComboCheckBox extends JComboBox<JCheckBox> {
private static final long serialVersionUID = 1;
public JComboCheckBox(LongFunction<String> message, JCheckBox... checkBoxes) {
super(checkBoxes);
addActionListener(event -> {
JCheckBox checkBox = (JCheckBox) getSelectedItem();
checkBox.setSelected(!checkBox.isSelected());
});
JLabel countLabel = new JLabel();
setRenderer((list, value, index, isSelected, cellHasFocus) -> {
if (index != -1)
return value;
int count = 0;
ComboBoxModel<JCheckBox> model = getModel();
for (int i = 0, n = model.getSize(); i < n; i++) {
if (model.getElementAt(i).isSelected())
count++;
}
countLabel.setText(message.apply(count));
return countLabel;
});
}
public static void main(String[] args) {
JFrame frame = new JFrame();
JComboCheckBox comboCheckBox = new JComboCheckBox(count -> count + " jour(s)",
new JCheckBox("Lundi"),
new JCheckBox("Mardi"),
new JCheckBox("Mercredi"),
new JCheckBox("Jeudi"),
new JCheckBox("Vendredi"),
new JCheckBox("Samedi", true),
new JCheckBox("Dimanche", true));
frame.add(comboCheckBox);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
