I have fxml and controller class. In controller class I have someAction() method and in fxml I set #someAction as onAction for some Button. Now I want to create button not in fxml but dynamically from java code as button = new Button(). I still load fxml and I still have this controller with someAction() method. How can I set someAction() as onAction for my button? From java code I found only setOnAction(EventHandler<ActionEvent> eh). Is there a way to specify onAction in fxml-like style, just telling method name in controller class?
Add a comment |
1 Answer
One of the approaches can be:
private EventHandler<ActionEvent> yourHandler = new EventHandler<>() { public void handle(ActionEvent event) { // your logic } }; then
button.setOnAction(yourHandler); and
public void someAction(ActionEvent event) { yourHandler.handle(null); } Or in the same manner, define a business logic method and call it in two different action event handlers.
2 Comments
ItachiUchiha
The second approach sounds more practical :)
Uluk Biy
@ItachiUchiha yes you are right. I just want also to show the usage of eventhandler here.