NBKRIST-JavaHub — JavaFX Event Handling

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.

MouseEvent
ActionEvent
KeyEvent

📘 What is ActionEvent?

ActionEvent represents a semantic action, such as activating a Button (mouse click or ENTER/SPACE when focused), choosing a MenuItem, or clicking a Hyperlink.

🎯 Why is it important?

It abstracts input method (mouse/keyboard), improves accessibility, and keeps your code focused on what happened, not how it happened.

🧩 How does it help GUI programming?

Use it to trigger form submissions, navigation, save actions, and other UI commands consistently across mouse and keyboard.

🌍 Real‑world uses

  • Submit/Save/Cancel buttons
  • Toolbar/Context actions via MenuItem
  • Hyperlink navigation

⚙️ Listener methods

SourceListenerEvent Type
ButtonsetOnAction()ActionEvent.ACTION
MenuItemsetOnAction()ActionEvent.ACTION
HyperlinksetOnAction()ActionEvent.ACTION

🧪 Step‑by‑Step Procedure

  1. Create a Button (event source).
  2. Create a handler class that implements EventHandler<ActionEvent>.
  3. Register the handler with button.setOnAction(new Handler()).
  4. Write action logic inside handle(ActionEvent e).

🔡 Example (beginner — named class)

Button btn = new Button("Submit");
btn.setOnAction(new SubmitHandler());

class SubmitHandler implements EventHandler<ActionEvent> {
  public void handle(ActionEvent e) {
    System.out.println("Form submitted!");
  }
}
Expected output: Console prints Form submitted! when the button is activated (mouse or keyboard).

🚀 Delegation Event Model (ActionEvent)

Button (Source) ActionEvent Listener setOnAction Handler.handle()

🏆 Will learning ActionEvent help you?

Yes. It teaches you to write accessible UI actions that work with both mouse and keyboard, a key skill for professional apps.

✅ Practice Task

Create two buttons: Save and Reset. Implement two handler classes. Show different console messages when each is activated.

📘 What is MouseEvent?

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.

🎯 Why is it important?

It turns static UI into interactive experiences: drawing, dragging, selecting, hovering, context menus, and more.

🧩 How does it help GUI programming?

Supports free‑form gestures and precision control not covered by semantic events — crucial for graphics tools and games.

🌍 Real‑world uses

  • Paint tools and diagram editors
  • Drag‑and‑drop reordering
  • Game input (aim, move)
  • Interactive charts (zoom/inspect)

⚙️ Listener methods

ListenerWhen 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

🧪 Step‑by‑Step Procedure

  1. Create a Node (e.g., Pane).
  2. Create a handler class that implements EventHandler<MouseEvent>.
  3. Register it with node.setOnMouseClicked(new Handler()) (or other mouse listeners).
  4. Use getX()/getY(), getButton(), and getClickCount() inside handle().

🔡 Example (named class)

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());
  }
}
Expected output: Console prints the coordinates of the click within the Pane.

🚀 Delegation Event Model (MouseEvent)

Node (Pane) MouseEvent Listener setOnMouse... Handler.handle()

🏆 Will learning MouseEvent help you?

Yes. You’ll be able to build drawing tools, interactive visualizations, and engaging UI mechanics — vital for modern apps.

✅ Practice Task

Write a handler that prints the button used (PRIMARY/SECONDARY) and shows “Double Click!” when getClickCount()==2.

📘 What is KeyEvent?

KeyEvent occurs when the user presses, releases, or types keys while a focused Node (or the Scene) is active.

🎯 Why is it important?

Keyboard shortcuts speed up workflows, enable accessibility, and make apps efficient for power users.

🧩 How does it help GUI programming?

Use it to implement hotkeys (e.g., Ctrl+S), navigation (arrow keys), and text input behavior.

🌍 Real‑world uses

  • Enter to submit forms
  • Arrow keys to move focus or objects
  • Shortcuts like Ctrl+S / Ctrl+Z

⚙️ Listener methods

ListenerWhen it Fires
setOnKeyPressed()Key pressed down
setOnKeyReleased()Key released
setOnKeyTyped()Textual character produced

🧪 Step‑by‑Step Procedure

  1. Create/focus a Node (e.g., TextField) or register on the Scene.
  2. Create a handler class implementing EventHandler<KeyEvent>.
  3. Register with setOnKeyPressed/Released/Typed.
  4. Read e.getCode() (for non‑text keys) or e.getCharacter() (typed).

🔡 Example (named class on Scene)

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());
  }
}
Expected output: Console prints the key code (e.g., ENTER, LEFT) when pressed while the window is focused.

🚀 Delegation Event Model (KeyEvent)

Node/Scene KeyEvent Listener setOnKey... Handler.handle()

🏆 Will learning KeyEvent help you?

Yes. You’ll implement productive shortcuts and accessible navigation — essential for professional UIs.

✅ Practice Task

Register a handler that moves a rectangle left/right with arrow keys and prints the new X position.