mercredi 5 septembre 2018

JavaFX removing performed action if checkbox is unselected

I am a beginner messing around with JavaFX. In my program, when a checkbox is selected I want it to display a label along with a ChoiceBox. However, when it is unselected, I want both of these to disappear. I'm not quite sure how to do this however.

Here is my code:

String [] options = new String [] {"A", "B", "C", "D", "E", "F"};
CheckBox [] cbs = new CheckBox[options.length];

    for (int i = 0; i < options.length; i++){
        final CheckBox cb = cbs[i] = new CheckBox(options[i]);
        cb.selectedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if(observable.getValue() == true){
                    ChoiceBox<Integer> choice = new ChoiceBox<>();
                    Label label = new Label("How many of the selected accounts do you have?");
                    choice.getItems().addAll(1, 2, 3, 4, 5);
                    choice.setValue(1);
                    selection.setAlignment(Pos.BOTTOM_LEFT);
                    selection.getChildren().addAll(label, choice);
                    gp.add(selection, 0, 8);
                } else if (observable.getValue() == false){
                    // remove above block if getValue() == false;
                }
            }
       });
   }




Aucun commentaire:

Enregistrer un commentaire