
Envoyé par
laurentg2017
chb cannot be resolved
Cela veut dire que la variable chb n'existe pas dans la portée où on l'utilise.
Avec une bonne indendation, on voit tout de suite le problème :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| for (int i = 0; i < questions.length; i++) {
System.out.println(i);
this.question = questions[i];
this.id = i;
JCheckBox cb = new JCheckBox(question);
Map<Integer, JCheckBox> chb = new HashMap<Integer, JCheckBox>();
chb.put(i, cb);
/*
* ListOfJCheckBox= new ArrayList<JCheckBox>(); jcb = new JCheckBox(question);
* ListOfJCheckBox.add(i,jcb);
*/
}
for (int cle : chb.keySet()) {
System.out.println("-" + cle);
} |
La variable chb est créée dans le bloc de la première boucle for. Elle ne peut donc exister dans le bloc de la seconde boucle for.
Créer la Map dans la boucle for n'a aucun intérêt : tu ne peux y associer qu'une JCheckBox avec un seul numéro et après, quand ça passe à l'itération suivante, la map est perdue, donc l'association aussi. Ton code pourrait compiler et la map être correctement remplie si tu sortais la déclaration (et l'initialisation) de la variable hors de la boucle :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public JCheckBoxInit() {// super(questions[number]);
Map<Integer, JCheckBox> chb = new HashMap<Integer, JCheckBox>();
for (int i = 0; i < questions.length; i++) {
System.out.println(i);
this.question = questions[i];
this.id = i;
JCheckBox cb = new JCheckBox(question);
chb.put(i, cb);
/*
* ListOfJCheckBox= new ArrayList<JCheckBox>(); jcb = new JCheckBox(question);
* ListOfJCheckBox.add(i,jcb);
*/
}
for (int cle : chb.keySet()) {
System.out.println("-" + cle);
}
} |
Mais ça ne servirait à rien pour ce que tu cherches à faire, parce que la variable ne serait pas accessible hors du constructeur et la map n'existerait plus après la construction de l'objet. Il faut plutôt faire comme ça, par exemple :
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
| import java.util.AbstractList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.swing.JCheckBox;
public class JCheckBoxMap implements Iterable<JCheckBox> {
private final Map<Integer, JCheckBox> map;
private final List<JCheckBox> list;
public JCheckBoxMap(String...questions) {
map = new TreeMap<>();
for(int i=0; i<questions.length; i++) {
map.put(i, new JCheckBox(questions[i]));
}
list = new AbstractList<JCheckBox>() {
@Override
public JCheckBox get(int index) {
if ( index<0 || index>=size() ) {
throw new IndexOutOfBoundsException("Index " +index + " is out of bounds [0,"+size()+"[");
}
return map.get(index);
}
@Override
public int size() {
return map.size();
}
};
}
public int count() {
return map.size();
}
public int size() {
return map.size();
}
public JCheckBox getCheckBox(int index) {
return map.get(index);
}
public boolean isSelected(int index) {
return getCheckBox(index).isSelected();
}
public void setSelected(int index, boolean selected) {
getCheckBox(index).setSelected(selected);
}
public String getQuestion(int index) {
return getCheckBox(index).getText();
}
@Override
public Iterator<JCheckBox> iterator() {
return map.values().iterator();
}
public List<JCheckBox> asList() {
return Collections.unmodifiableList(list);
}
public Set<Map.Entry<Integer,JCheckBox>> asSet() {
return Collections.unmodifiableSet(map.entrySet());
}
} |
Du coup tu as juste à faire :
Pour la déclaration
Pour l'initialisation :
checkboxes = new JCheckBoxMap("Une tristesse presque quotidienne, souvent toute la journée","Pas ou peu d'interêt pour quasiment toutes les activités et cela quotidiénement","Variation de l'appétit, de moins important à plus important","Problème de sommeil","Fatigue");
Pour traiter une checkbox par son numéro :
panel.add(checkboxes.getCheckBox(2));
Traitement par for :
1 2 3
| for(int i=0; i<checkboxes.size(); i++) {
panel.add(checkboxes.getCheckBox(i));
} |
par forEach :
1 2 3
| for(JCheckBox : checkboxes) {
panel.add(checkboxes);
} |
si tu as besoin d'une liste :
List<JCheckBox> liste = checkboxes.asList();
de traiter les checkbox avec leur numéro :
1 2 3 4
|
for(Map.Entry<Integer,JCheckBox> checkbox : checkboxes.asSet()) {
checkbox.getValue().addActionPerformed(e-> System.out.println("checkbox numéro "+ checkbox.getKey() +" : " + (checkbox.getValue().isSelected()?"":"pas") + " cochée" ));
} |
etc...
Partager