1

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

Malek Boubakri
  • 820
  • 2
  • 17
  • 36

1 Answers1

1

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)

Elltz
  • 10,730
  • 4
  • 31
  • 59
  • Hmm, yes you are right. This is the problem, do you suggest any other idea to do that.. – Malek Boubakri Oct 17 '15 at 18:00
  • Thanks, i do mark your answer as accepted cause it help me avoid the eception.. but a have no other idea.Do you suggest any approach? – Malek Boubakri Oct 18 '15 at 14:57
  • 1
    oh sorry i will be right back with you-okay please@MalekBoubakri – Elltz Oct 18 '15 at 16:52
  • 1
    i took too much time off hmm. @MalekBoubakri – Elltz Oct 21 '15 at 16:21
  • 1
    please in short what do you want,for eg, you said you have list {1,2,3} and when you select `1` you want get some list lik {11,111,111} and also if you select `2` then {222,22,222}. that is your problem right? but it doesn't work as such? i want to create a demo that is why i am asking@MalekBoubakri sorry for these long story, and please unaccept it for now. – Elltz Oct 21 '15 at 18:22
  • Thanks @Elltz but i think i get passed this problem yesterday with another approach based on your answer, if want really help me you can check those problems and i would be greatful --> [link](http://stackoverflow.com/questions/33194517/javafx-how-to-set-a-titeledpane-selected-opened-by-default) . As you can see i have no knowledge or skills in javaFX.. but i try to do my best – Malek Boubakri Oct 21 '15 at 18:54
  • 1
    okay sir i will help you, don't worry we all start from ground 0. im not much an expert though(joking) people like me help stabilize the world :)@MalekBoubakri – Elltz Oct 21 '15 at 22:41