0

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The second approach sounds more practical :)
@ItachiUchiha yes you are right. I just want also to show the usage of eventhandler here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.