Event handlers
Event handlers are implementations of the javafx.event.EventHandler interface, and can be set on event handler properties with an fx:Evaluate or fx:Observe expression:
com/sample/MyControl.fxml
<StackPane xmlns="http://javafx.com/javafx" xmlns:fx="http://jfxcore.org/fxml/2.0"
fx:subclass="com.sample.MyControl">
<Button onAction="$myActionHandler"/>
</StackPane>
com/sample/MyControl.java
public class MyControl extends MyControlBase {
final EventHandler<ActionEvent> myActionHandler = (event) -> {
...
};
public MyControl() {
initializeComponent();
}
}
Usually, event handlers don’t change dynamically. It is therefore advisable to use fx:Evaluate instead of fx:Observe.
Method event handlers
Event handlers can also be implemented as methods on the code-behind class that have a signature compatible with the javafx.event.EventHandler interface. The method name must be prefixed with # in the FXML attribute:
com/sample/MyControl.fxml
<StackPane xmlns="http://javafx.com/javafx" xmlns:fx="http://jfxcore.org/fxml/2.0"
fx:subclass="com.sample.MyControl">
<!-- Note that # identifies a method handler -->
<Button onAction="#handleActionEvent"/>
</StackPane>
com/sample/MyControl.java
public class MyControl extends MyControlBase {
public MyControl() {
initializeComponent();
}
void handleActionEvent(ActionEvent event) {
...
}
}
The event parameter is optional in the method signature and can be omitted if not required.