Calling method in Choicebox

Asked

Viewed 24 times

0

I created a Choicebox, where at each selected level, a method should be called; but the following happens, any selected item calls the method; how to make a condition, that is, depending on the selected item, calls a certain method. Follow a part of the code.

ChoiceBox cb = new ChoiceBox();
    cb.setId("btn_transparent_cb");
    cb.getItems().addAll("Nível 1", "Nível 2", "Nível 3", "Nível 4");
    //cb.getSelectionModel().selectFirst();


    cb.getSelectionModel().selectedIndexProperty()
    .addListener(new ChangeListener<Number>() {
      @Override
      public void changed(ObservableValue ov, Number value, Number new_value) {

        exemploMetodo();
      }
    });

1 answer

0


Resolvi, guys; thanks. Follow the solution:

 cb.getSelectionModel().selectedIndexProperty().addListener((obs, old , newValue)->{
        switch(newValue.intValue()){
            case 0:
                metodoUm();
                break;
            case 1:
                metodoDois();
                break;
            case 2:
                metodoTres();
                break;
        }
    });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.