Search code examples
javafxevent-handlingaddeventlistenerdropdownchoice

How to listen to on selected event in choice box javafx


Im using scenebuilder and I have come up with 3 choiceboxes. The second choicebox depends on the input of the first choicebox and the third depends on the 2nd. How can I achieve this?

I've tried this

@FXML
private ChoiceBox  course;

course.getSelectionModel().selectedIndexProperty().addListener(
        (ObservableValue<? extends Number> ov,
             Number old_val, Number new_val) -> { 
                //some code here
            }
    );

But this event only occurs if i switch value, the first selection would not trigger this event, which is not what I want. How can I achieve this, thanks in advance.


Solution

  • You can do something like this where everytime an action is done it will set the values of the next one. Make note of the .getItems().clear(); this will ensure the list is emptied everytime so that you don't have old values in the list. The for loop however is not important only there to add some variety to the text values I added

    public class Main extends Application {
    
        @Override
        public void start(Stage primaryStage) {
            ChoiceBox<String> choiceBoxOne = new ChoiceBox<>();
            choiceBoxOne.setPrefWidth(100);
            choiceBoxOne.getItems().addAll("Choice1", "Choice2", "Choice3");
    
            ChoiceBox<String> choiceBoxTwo = new ChoiceBox<>();
            choiceBoxTwo.setPrefWidth(100);
    
            ChoiceBox<String> choiceBoxThree = new ChoiceBox<>();
            choiceBoxThree.setPrefWidth(100);
    
            choiceBoxOne.setOnAction(event -> {
                choiceBoxTwo.getItems().clear();
                //The above line is important otherwise everytime there is an action it will just keep adding more
                if(choiceBoxOne.getValue()!=null) {//This cannot be null but I added because idk what yours will look like
                    for (int i = 3; i < 6; i++) {
                        choiceBoxTwo.getItems().add(choiceBoxOne.getValue() + i);
                    }
                }
            });
    
            choiceBoxTwo.setOnAction(event -> {
                choiceBoxThree.getItems().clear();
                //The above line is important otherwise everytime there is an action it will just keep adding more
                if(choiceBoxTwo.getValue()!=null) {//This can be null if ChoiceBoxOne is changed
                    for (int i = 6; i < 9; i++) {
                        choiceBoxThree.getItems().add(choiceBoxTwo.getValue() + i);
                    }
                }
            });
    
    
            VBox vBox = new VBox();
            vBox.setPrefSize(300, 300);
            vBox.setAlignment(Pos.TOP_CENTER);
            vBox.getChildren().addAll(choiceBoxOne, choiceBoxTwo, choiceBoxThree);
    
            primaryStage.setScene(new Scene(vBox));
            primaryStage.show();
        }
    
        public static void main(String[] args) { launch(args); }
    }