You can explore Bindings.
There is example for 2 checkBox selected:
BooleanProperty bp = new SimpleBooleanProperty(false);
CheckBox cb1 = new CheckBox();
CheckBox cb2 = new CheckBox();
bp.bind(Bindings.and(cb1.selectedProperty(), cb2.selectedProperty()));
bp.addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
}
});
For multiple CheckBoxes some wierd idea arrived. I hope could explain it.
The main idea like unix chmod 000 - no access, 777 - all access.
For example you have five CBoxes. Let add some value to each.
CB1 value 1
CB2 value 2
CB3 value 3
CB4 value 4
CB5 value 5
And use someVariable to store total value of all selected CBoxes.
When none selected: someVariable == 0.
When all selected: someVariable == (1+2+3+4+5)
IntegerProperty checkedNumbers = new SimpleIntegerProperty(0);
checkedNumbers.addListener(new javafx.beans.value.ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
if (newValue == 0) {
//doSomething
} else if (newValue == (1 + 2 + 3 + 4 + 5)) {
//doSomethingElse
}
}
});
int cBoxNumbers = 5;
//Implement same ChangeListener for every CBoxes based on different UserData.
//When CB selected, add its value to checkedNumbers
//When unselected - subtract
ChangeListener changeListener;
for (int i = 1; i <= cBoxNumbers; i++) {
CheckBox cb1 = new CheckBox();
cb1.setUserData(i);
cb1.selectedProperty().addListener(changeListener);
}