I'm detecting the change of a button group's value with a Listener
. If there is nothing selected I put the previous value before the change, but this causes me to call back to Listener
How can I avoid it?
The code is:
toggleGroup.selectedToggleProperty().addListener(
(ObservableValue<? extends Toggle> arg0, Toggle oldValue, Toggle newValue) -> {
if (toggleGroup.getToggles().indexOf(toggleGroup.getSelectedToggle()) == -1) {
toggleGroup.selectToggle(oldValue); // esto provoca rellamada al Listener
}
}
);
You can create a flag that indicates if you have tried to override all the options of the
toggleGroup
:EDIT: what I describe below after the line seems incorrect. I put another answer.
If you save in a global variable the value you are assigning you can avoid a second call:
I don't know how to prevent the callback of the listener being in its own declaration, but you can end this recursion with a base condition.
If
oldValue = newValue
then you don't change anything and thelistener
will not launch: