2

I want to add a custom action in my new component. How to do this?

Example code:

Component

public class MyCustomComponent extends Region { public MyCustomComponent(){ super(); this.setOnMouseClicked(new EventHandler<MouseEvent>(){ @Override public void handle(MouseEvent event) { /* throw my custom event here and handle it in my FXML controller - but how? :-( */ } }); } } 

Controller

public class MyController { @FXML protected void myCustomAction(ActionEvent event) { // do something } } 

FXML:

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

Thx for help

1 Answer 1

6

You need to implement property in your custom component, which will store your action.

public class MyCustomComponent extends Region { public MyCustomComponent(){ super(); // just to find out where to click setStyle("-fx-border-color:red;"); setPrefSize(100, 100); this.setOnMouseClicked(new EventHandler<MouseEvent>(){ @Override public void handle(MouseEvent event) { onActionProperty().get().handle(event); } }); } // notice we use MouseEvent here only because you call from onMouseEvent, you can substitute any type you need private ObjectProperty<EventHandler<MouseEvent>> propertyOnAction = new SimpleObjectProperty<EventHandler<MouseEvent>>(); public final ObjectProperty<EventHandler<MouseEvent>> onActionProperty() { return propertyOnAction; } public final void setOnAction(EventHandler<MouseEvent> handler) { propertyOnAction.set(handler); } public final EventHandler<MouseEvent> getOnAction() { return propertyOnAction.get(); } } 

and don't forget to add import in your fxml file:

<?import my.package.MyCustomComponent?> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.