vendredi 27 juillet 2018

How to limit selection of checkboxes, based on selected radiobutton (JAVA FX)

Is it possible to limit the number of checkboxes that can be selected, based on a radiobox that the user selects in the same scne? In my code, there are 2 radiobuttons and 5 color checkboxes. You can only select one radiobutton. If "dog" is selected, I only want the user to be able to select 2 colors. If "cat" is selected, I only want the user to be able to select 3 colors.

    GridPane lay1 = new GridPane();
    lay1.setHgap(0);
    lay1.setVgap(20);

    RadioButton dog = new RadioButton("dog");
    RadioButton cat = new RadioButton("cat");
    ToggleGroup type = new ToggleGroup();
    dog.setToggleGroup(type);
    cat.setToggleGroup(type);
    VBox types = new VBox(20);
    types.getChildren().addAll(dog,cat);
    lay1.add(types, 0, 3);

    CheckBox cb1 = new CheckBox("red");
    CheckBox cb2 = new CheckBox("yellow");
    CheckBox cb3 = new CheckBox("black");
    CheckBox cb4 = new CheckBox("white");
    CheckBox cb5 = new CheckBox("green");

    VBox colors = new VBox(15);
    colors.getChildren().addAll(cb1, cb2, cb3, cb4, cb5);
    lay1.add(colors, 0, 4);`




Aucun commentaire:

Enregistrer un commentaire