1

I created a new custom control containing labels, text field and a button. How can I set custom control's Button "onAction" method in the FXML file?

Example code:

<BorderPane fx:controller="fxmlexample.MyController" xmlns:fx="http://javafx.com/fxml"> <top> <MyCustomComponent onButtonAction="#myCustomButtonAction"> </MyCustomComponent> </top> 

1
  • You need to expose a method from your custom control which fire's the button and then use it in FXML. Commented Aug 12, 2016 at 11:39

2 Answers 2

0

Ok, I found a solution. I did it exactly as Sergey Grinev suggested in this thread - JavaFX 2.0 - create action handler for custom component in FXML. The only change I made was using my button's field name instead of "this" in Class constructor.

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

Comments

0

onButtonAction="#myCustomButtonAction" can be used as long as MyCustomComponent contains a setter for the event handler named setOnButtonAction.

class MyCustomComponent { private Button button; public void setOnButtonAction(EventHandler<ActionEvent> handler) { this.button.setOnAction(handler); } public EventHandler<ActionEvent> getOnButtonAction() { return this.button.getOnAction(); } ... } 

2 Comments

Unfortunately, this did not work. But I found a solution (in my answer).
@notmyf4ulty sorry, forgot that the getter is required for the FXMLLoader to get handler type. Should work now...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.