ComboBox : Récupérer l'objet correspondant à la sélection.
Bonjour,
J'utilise une ComboBox pour pouvoir sélectionner un objet en DB. Je passe donc par un objet "ComboBoxChoice" qui encapsule simplement l'ID de l'objet ainsi qu'un CODE lisible qui permet à l'utilisateur de l'identifier.
Code:
1 2 3
|
@FXML
private ComboBox<ComboBoxChoice> colDomain; |
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
|
public class ComboBoxChoice {
private Integer id;
private String displayString;
public ComboBoxChoice(Integer id, String displayString){
this.id = id;
this.displayString = displayString;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDisplayString() {
return displayString;
}
public void setDisplayString(String displayString) {
this.displayString = displayString;
}
@Override
public String toString(){
return "AAA" +displayString;
}
} |
Je peuple donc mon ComboBox.
Code:
1 2 3
|
List<ComboBoxChoice> choices = defDomainDAO.getAllComboBoxChoices();
colDomain.setItems(FXCollections.observableArrayList(choices)); |
Pourtant lorsque j'appelle cette méthode, alors que mon IDE me dit bien que getSelectedItem() doit me renvoyer un objet de type ComboBoxChoice, en déguggant, je me rends compte que je reçoit un objet de type String et cela provoque une erreur au Runtime.
Code:
1 2
|
ComboBoxChoice c = colDomain.getSelectionModel().getSelectedItem(); |
Et si j'essaye de récupérer la valeur dans un String, c'est évidemment mon compilateur qui râle ...
Code:
1 2
|
String s = colDomain.getSelectionModel().getSelectedItem(); |
Est-ce que quelqu'un a une idée d'où vient le problème ?
Bien à vous.
Lionel