This page teaches the JavaFX Delegation Event Model for three core event types — ActionEvent, MouseEvent, and KeyEvent. The content is beginner‑friendly. According to your syllabus, you should focus on understanding MouseEvent Events thoroughly.
ActionEvent represents a semantic action, such as activating a Button (mouse click or ENTER/SPACE when focused), choosing a MenuItem, or clicking a Hyperlink.
It abstracts input method (mouse/keyboard), improves accessibility, and keeps your code focused on what happened, not how it happened.
Use it to trigger form submissions, navigation, save actions, and other UI commands consistently across mouse and keyboard.
| Source | Listener | Event Type |
|---|---|---|
| Button | setOnAction() | ActionEvent.ACTION |
| MenuItem | setOnAction() | ActionEvent.ACTION |
| Hyperlink | setOnAction() | ActionEvent.ACTION |
EventHandler<ActionEvent>.button.setOnAction(new Handler()).handle(ActionEvent e).Button btn = new Button("Submit");
btn.setOnAction(new SubmitHandler());
class SubmitHandler implements EventHandler<ActionEvent> {
public void handle(ActionEvent e) {
System.out.println("Form submitted!");
}
}
Yes. It teaches you to write accessible UI actions that work with both mouse and keyboard, a key skill for professional apps.
Create two buttons: Save and Reset. Implement two handler classes. Show different console messages when each is activated.
MouseEvent occurs when the user interacts with the mouse over a JavaFX Node (Pane, Button, Circle, ImageView, etc.). Contains position (X,Y), button used, click count, and modifier key state.
JavaFX MouseEvent(package: javafx.scene.input.MouseEvent) represents mouse interactions like moving, pressing, releasing, clicking, dragging, entering, and exiting a node (any GUI element that can receive user interaction). It provides detailed info such as cursor coordinates, button type, click count, and modifier keys to enable precise handling of user actions.
It turns static UI into interactive experiences: drawing, dragging, selecting, hovering, context menus, and more.
Supports free‑form gestures and precision control not covered by semantic events — crucial for graphics tools and games.
| Listener | When it Fires |
|---|---|
| setOnMousePressed() | Mouse button pressed |
| setOnMouseReleased() | Mouse button released |
| setOnMouseClicked() | Press + release on same node (supports double‑click via getClickCount()) |
| setOnMouseMoved() | Pointer moves without button down |
| setOnMouseDragged() | Pointer moves while button is down |
| setOnMouseEntered()/Exited() | Pointer crosses node boundary |
| setOnScroll() | Mouse wheel / touchpad scroll |
Pane).EventHandler<MouseEvent>.node.setOnMouseClicked(new Handler()) (or other mouse listeners).getX()/getY(), getButton(), and getClickCount() inside handle().Pane canvas = new Pane();
canvas.setOnMouseClicked(new CanvasClick());
class CanvasClick implements EventHandler<MouseEvent> {
public void handle(MouseEvent e) {
System.out.println("Clicked at X="+e.getX()+" Y="+e.getY());
}
}
Yes. You’ll be able to build drawing tools, interactive visualizations, and engaging UI mechanics — vital for modern apps.
Write a handler that prints the button used (PRIMARY/SECONDARY) and shows “Double Click!” when getClickCount()==2.
KeyEvent occurs when the user presses, releases, or types keys while a focused Node (or the Scene) is active.
Keyboard shortcuts speed up workflows, enable accessibility, and make apps efficient for power users.
Use it to implement hotkeys (e.g., Ctrl+S), navigation (arrow keys), and text input behavior.
| Listener | When it Fires |
|---|---|
| setOnKeyPressed() | Key pressed down |
| setOnKeyReleased() | Key released |
| setOnKeyTyped() | Textual character produced |
TextField) or register on the Scene.EventHandler<KeyEvent>.setOnKeyPressed/Released/Typed.e.getCode() (for non‑text keys) or e.getCharacter() (typed).Scene scene = new Scene(root);
scene.setOnKeyPressed(new GlobalKeys());
class GlobalKeys implements EventHandler<KeyEvent> {
public void handle(KeyEvent e) {
System.out.println("Key Pressed: " + e.getCode());
}
}
Yes. You’ll implement productive shortcuts and accessible navigation — essential for professional UIs.
Register a handler that moves a rectangle left/right with arrow keys and prints the new X position.