Event handling in JavaFX is the mechanism that lets your application react to user input and other occurrences, such as button clicks, key presses and mouse movement. You attach an EventHandler to a node, either with a convenience method like setOnAction or with addEventHandler, and JavaFX calls it when a matching event is delivered to that node.
Events travel through the scene graph in two phases: a capturing phase from the root down to the target, then a bubbling phase back up. Understanding this lets you handle events at the right level and consume them when needed. JavaFX is maintained as the OpenJFX project and is no longer bundled with the JDK, so add it as a dependency.
Every event is an instance of javafx.event.Event or a subclass. Each has an EventType that identifies exactly what happened.
| Event class | Typical event types | Triggered by |
|---|---|---|
ActionEvent | ACTION | Button press, menu item, text field Enter |
MouseEvent | MOUSE_CLICKED, MOUSE_PRESSED, MOUSE_ENTERED | Mouse activity |
KeyEvent | KEY_PRESSED, KEY_RELEASED, KEY_TYPED | Keyboard |
ScrollEvent | SCROLL | Wheel or touchpad scroll |
WindowEvent | WINDOW_CLOSE_REQUEST | Window lifecycle |
Focus changes are not events but property changes: observe focusedProperty() on a node with a listener.
The simplest approach is a convenience method named setOn<EventName>. It holds a single handler per event kind, and a later call replaces the earlier one.
EventHandler is a functional interface, so lambdas and method references work. When a handler grows, move it into a method and use button.setOnAction(this::save).
To register several handlers for the same event type, or to handle a type that has no convenience method, use addEventHandler. It returns nothing, and you remove the handler with removeEventHandler.
An event filter, added with addEventFilter, runs during the capturing phase, before the target sees the event. Filters can inspect or consume events early.
When an event occurs, JavaFX builds an event dispatch chain from the stage down to the target node.
Calling event.consume() stops further propagation, so parents' handlers do not run. A mouse click on a button inside a VBox will reach the button's handler first and then the VBox handler, unless the button consumed it.
This application updates a label from a text field when the user presses a button or hits Enter.
With OpenJFX on the module path, run it with the javafx.controls module, for example through the Maven javafx-maven-plugin or Gradle org.openjfx.javafxplugin. Handlers run on the JavaFX Application Thread, so keep them short and move slow work, such as network calls, to a background Task or virtual thread, then update the UI with Platform.runLater.
With FXML you declare the handler in markup and implement it in the controller:
In the FXML file the button references it with onAction="#onSave".
A filter runs during the capturing phase before the target, and a handler runs at the target or during bubbling. Use filters to intercept events early.
The handler runs on the Application Thread. Long work blocks rendering, so run it in a Task and update the interface with Platform.runLater.
Call event.consume() in a handler or filter.
Yes. Add OpenJFX as a dependency since it is no longer part of the JDK, and use a release compatible with your JDK version.
Attach handlers with setOnAction or addEventHandler, use filters for early interception, remember the capture and bubble phases, call consume() to stop propagation, and keep handlers fast so the Application Thread stays responsive.