1

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.

Tyson Reese
  • 53
  • 1
  • 6
  • 1
    First, you should [avoid using raw types](https://stackoverflow.com/questions/2770321). `ChoiceBox` has a generic parameter. Second, I would assume if you added the listeners _before_ the first selection then everything should work properly. If that doesn't help, please [edit] your question to add a [mcve] demonstrating the issue. – Slaw May 10 '19 at 10:25
  • Thanks for the advice. But how exactly do you add listener on the choicebox but before the selection? – Tyson Reese May 10 '19 at 15:16

1 Answers1

0

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); }
}
Matt
  • 3,052
  • 1
  • 17
  • 30