Evgenij Reznik
Evgenij Reznik

Reputation: 18594

Getting all selected toggle buttons from same group

I have 3 toggle buttons (FXML):

<ToggleButton fx:id="toggle1" text="Option 1" />
<ToggleButton fx:id="toggle2" text="Option 2" />
<ToggleButton fx:id="toggle3" text="Option 3" />

Those are several options the user can select or deselect. Multiple selection should also be possible.
In order to get all selected toggles, I put them into one toggle group:

final ToggleGroup toggleGroup = new ToggleGroup();

toggle1.setToggleGroup(toggleGroup);
toggle2.setToggleGroup(toggleGroup);
toggle3.setToggleGroup(toggleGroup);

But, in that case, only 1 button at a time can be selected, not 2 or 3.

How to get all selected toggle buttons from the same group? So, basically, same principle as with check boxes.

Upvotes: 0

Views: 1336

Answers (1)

Miss Chanandler Bong
Miss Chanandler Bong

Reputation: 4258

You can use a CheckBox and make it look like a radio button using CSS. To get all selected checkboxes, you can create an observable collection like ObservableSet and assign listeners to the checkboxes, when a checkbox is selected add it to the collection and remove it when it gets unselected

ObservableSet<CheckBox> selectedCheckBoxes = FXCollections.observableSet();
CheckBox checkBox1 = new CheckBox();
checkBox1.selectedProperty().addListener((observable, oldValue, selectedNow) -> {
    if (selectedNow) {
        selectedCheckBoxes.add(checkBox1);
    } else {
        selectedCheckBoxes.remove(checkBox1);
    }
}); 

Upvotes: 2

Related Questions