Reputation: 17604
I am adding a listener using FXML:
<RadioButton onAction="#onSelectionChanged" />
Now I need to temporarily disable this listener programmatically.
Now I could set some boolean variable "listenerDisabled" and check this variable in the listener, but I want a way to disable a listener without changing it - so I want to remove it.
The problem here is: How do I reference the listener in my code, so I can use the following?
RadioButton.selectedProperty().removeListener(<what to place here?>)
Thanks for any hint!
Upvotes: 2
Views: 3740
Reputation: 159291
Add an fx:id specification to your fxml:
<RadioButton fx:id="myRadio" onAction="#onSelectionChanged" />
In the corresponding controller for the fxml, use the @FXML
notation to have the FXMLLoader
inject a reference to the radio button into your controller:
@FXML RadioButton myRadio;
To get a reference to the listener, invoke getOnAction
:
EventHandler<ActionEvent> myRadioActionEvent = myRadio.getOnAction();
To remove the listener, use setOnAction
:
myRadio.setOnAction(null);
To add the listener back again, use setOnAction
again:
myRadio.setOnAction(myRadioActionEvent);
I didn't try any of the above, but I don't see why it would not work.
Upvotes: 3