I am working on a project for my education that manage a primary school. i am trying to set a user interface with javaFX and i use also an SQLite DB.
The problem that when i set two comboBox, one for the student level cb_nv
and the other one cb_nb
for the classe number in the level selected. cb_nv
is initialized and work good, the second ComboBox should take the value lvl
returned from cb_nv
and should show only the classes existing in the BD.
here is my approach:
@FXML
ComboBox<String> cb_nb, cb_nv;
int lvl=1;
int nb=1;
ObservableList<String> optionslvl=FXCollections.observableArrayList("A","B","C","D");
ObservableList<String> optionsnb=bdClss.getNBListByLvl(lvl);
...
cb_nv.setItems(optionslvl);
cb_nv.setValue("A");
cb_nb.setItems(optionsnb);
cb_nb.setValue("1");
the list is succefully obtained from BD and showed in cb_nb
(lvl=1) with no problem, but when i change cb_nv
to "B" i still getting the "A" classe list.
here is the part of the code that should do the change:
cb_nv.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) {
nb=1;
cb_nb.setValue("1");
if(cb_nv.getItems().get((Integer) number2)=="A"){
lvl=0;
cb_nb.getItems().clear();
cb_nb.setItems(bdClss.getNBListByLvl(lvl));}
else if(cb_nv.getItems().get((Integer) number2)=="B"){
lvl=1;
cb_nb.getItems().clear();
cb_nb.setItems(bdClss.getNBListByLvl(lvl));}
else if(cb_nv.getItems().get((Integer) number2)=="C"){
lvl=2;
cb_nb.getItems().clear();
cb_nb.setItems(bdClss.getNBListByLvl(lvl));}
else if(cb_nv.getItems().get((Integer) number2)=="D"){
lvl=3;
cb_nb.getItems().clear();
cb_nb.setItems(bdClss.getNBListByLvl(lvl));}
else{System.err.println("Erreur lors de changement de class..");}
}
});
cb_nb.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) {
if(cb_nb.getItems().get((Integer) number2)=="1"){nb=1;}
else if(cb_nb.getItems().get((Integer) number2)=="2"){nb=2;}
else if(cb_nb.getItems().get((Integer) number2)=="3"){nb=3;}
else if(cb_nb.getItems().get((Integer) number2)=="4"){nb=4;}
else if(cb_nb.getItems().get((Integer) number2)=="5"){nb=5;}
else if(cb_nb.getItems().get((Integer) number2)=="6"){nb=6;}
else if(cb_nb.getItems().get((Integer) number2)=="7"){nb=7;}
else if(cb_nb.getItems().get((Integer) number2)=="8"){nb=8;}
else if(cb_nb.getItems().get((Integer) number2)=="9"){nb=9;}
else{System.err.println("Erreur lors de changement de class..");}
}
});
i need the returned nb
value in the end. But user should not choose a not existence classe cause next the program will go search it in the BD. i need to fix cb_nb
to show only valid choice..
EDIT:
The first Problem is solved, it shows the correct option now, but an Exception appear every time a move from a lvl with classes to another empty one.. and the if()
goes to final else{}
all the way!
Erreur lors de changement de class..
Exception in thread "JavaFX Application Thread" java.lang.ArrayIndexOutOfBoundsException
Any suggestion will help.. Should i think to change all the approach cause it seen too wrong.. Thanks
Rule no 1 ComBox
's selectionModel is different from all selectionmodels available, so if you use ComBox
and you have like 10 items, and you select the 4th item-with index 3, and you later clear your items and add 11 items, your selection is the 4th item in index 3. your value for the ComBox never resets unless you explicitly do that.
That being you have ArrayOutOfBoundsException
the index does not exist -(hope you have seen the problem)