Dr. Vatsal Shah
Subject Material

Java · JavaFX · Topic 5

Basic Event Handling

Understand event-driven programming and handle actions, text, selections, keyboard input, and mouse input.

Topic progress · 1 of 13 sections

Step 1 of 13

5.1 Event-Driven Programming

Learning objective

Contrast sequential console flow with event-driven GUI flow.

Programming flow
Console programEvent-driven JavaFX program
Runs statements in a planned sequenceStarts the interface and waits
Often blocks while reading inputReceives events from controls, keys, and the mouse
The program asks for the next valueThe user chooses the next action

After stage.show(), the JavaFX event loop continues running. When the user clicks, types, or changes a selection, JavaFX creates an event and dispatches it to a matching handler.

Check your understanding: What determines which handler runs in an event-driven application?

The event that occurs and the handler registered for its source or target.

Step 2 of 13

5.2 What is an Event?

Learning objective

Define an event and identify common JavaFX event categories.

Common events

  • ActionEvent for command controls
  • KeyEvent for keyboard input
  • MouseEvent for mouse actions
  • InputMethodEvent for composed text input
  • Selection and value changes exposed through observable properties
Check your understanding: What event commonly represents a Button activation?

ActionEvent.

Step 3 of 13

5.3 Event Source

Learning objective

Identify the object where an event originates.

Read an event source
submit.setOnAction(event -> {
    Button source = (Button) event.getSource();
    result.setText(source.getText() + " clicked");
});
Check your understanding: In a button-click example, what is normally the event source?

The Button that was activated.

Step 4 of 13

5.4 Event Object

Learning objective

Use event-object information inside a handler.

Inspect event details
scene.setOnKeyPressed(event ->
        status.setText("Key: " + event.getCode()));

root.setOnMouseClicked(event ->
        status.setText("Mouse: " + event.getX()
                + ", " + event.getY()));
Check your understanding: Which MouseEvent methods return the pointer position relative to the node?

getX() and getY().

Step 5 of 13

5.5 Event Handler

Learning objective

Describe the code executed in response to an event.

Register a handler
button.setOnAction(event -> {
    result.setText("Operation completed");
});

A useful handler usually

  • Reads the event or current control values
  • Validates input
  • Updates application state
  • Updates the interface
  • Reports errors clearly
Check your understanding: Does registering a handler execute it immediately?

No. It runs when the matching event is dispatched.

Step 6 of 13

5.6 EventHandler<ActionEvent>

Learning objective

Implement the EventHandler interface explicitly and compare it with a lambda.

Anonymous EventHandler object
button.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        result.setText("Clicked");
    }
});
Equivalent lambda expression
button.setOnAction(event -> result.setText("Clicked"));
EventHandlerDemo.java
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class EventHandlerDemo extends Application {
    @Override
    public void start(Stage stage) {
        Button button = new Button("Click Me");
        Label result = new Label("Waiting");
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                result.setText("Handled by EventHandler");
            }
        });
        stage.setScene(new Scene(new VBox(12, button, result), 340, 180));
        stage.setTitle("EventHandler Demo");
        stage.show();
    }
    public static void main(String[] args) { launch(args); }
}
Expected result
Before clicking: Waiting
After clicking: Handled by EventHandler
Check your understanding: Which method is implemented by EventHandler<ActionEvent>?

handle(ActionEvent event).

Step 7 of 13

5.7 Button Click Event

Learning objective

Handle a command button and validate numeric input.

Addition button event
addButton.setOnAction(event -> {
    try {
        double first = Double.parseDouble(firstField.getText());
        double second = Double.parseDouble(secondField.getText());
        result.setText("Sum: " + (first + second));
    } catch (NumberFormatException ex) {
        result.setText("Enter valid numbers");
    }
});
AdditionEventDemo.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class AdditionEventDemo extends Application {
    @Override
    public void start(Stage stage) {
        TextField first = new TextField();
        TextField second = new TextField();
        Button add = new Button("Add");
        Label result = new Label();
        add.setOnAction(e -> {
            try {
                double a = Double.parseDouble(first.getText());
                double b = Double.parseDouble(second.getText());
                result.setText("Sum: " + (a + b));
            } catch (NumberFormatException ex) {
                result.setText("Enter valid numbers");
            }
        });
        stage.setScene(new Scene(new VBox(10, first, second, add, result), 340, 240));
        stage.setTitle("Addition Event Demo");
        stage.show();
    }
    public static void main(String[] args) { launch(args); }
}
Expected result
For inputs 15 and 25:
Sum: 40.0

For invalid input:
Enter valid numbers
Check your understanding: Which exception should be handled when converting invalid text to a number?

NumberFormatException.

Step 8 of 13

5.8 TextField Events

Learning objective

Respond when the user submits text or when the text value changes.

Submit with Enter
nameField.setOnAction(event ->
        greeting.setText("Hello " + nameField.getText()));
Observe every text change
nameField.textProperty().addListener(
        (observable, oldValue, newValue) ->
                count.setText("Characters: " + newValue.length()));
Check your understanding: How can a handler run when the user presses Enter in a TextField?

Register it with setOnAction().

Step 9 of 13

5.9 Checkbox Events

Learning objective

Respond to independent selected-state changes.

Update selected subjects
EventHandler<ActionEvent> updateSubjects = event -> {
    String selected = "";
    if (javaBox.isSelected()) selected += "Java ";
    if (pythonBox.isSelected()) selected += "Python ";
    result.setText("Subjects: " + selected.trim());
};

javaBox.setOnAction(updateSubjects);
pythonBox.setOnAction(updateSubjects);
Check your understanding: Can several CheckBox handlers report selected choices at the same time?

Yes, because CheckBoxes are independent.

Step 10 of 13

5.10 RadioButton Events

Learning objective

Respond to a mutually exclusive selection and safely read the selected toggle.

Report the selected option
EventHandler<ActionEvent> showGender = event -> {
    RadioButton selected =
            (RadioButton) genderGroup.getSelectedToggle();
    result.setText(selected == null
            ? "Select gender"
            : "Gender: " + selected.getText());
};

male.setOnAction(showGender);
female.setOnAction(showGender);
Check your understanding: Why must getSelectedToggle() be checked for null?

A group may have no selected RadioButton.

Step 11 of 13

5.11 ComboBox Event

Learning objective

Respond when a new drop-down item is chosen.

Display the selected branch
branchBox.setOnAction(event -> {
    String branch = branchBox.getValue();
    result.setText(branch == null
            ? "Select a branch"
            : "Branch: " + branch);
});
Check your understanding: What must be handled before using the selected value?

The possibility that getValue() is null.

Step 12 of 13

5.12 Keyboard Events

Learning objective

Handle pressed, released, and typed keyboard input.

Show the pressed key
scene.setOnKeyPressed(event -> {
    status.setText("Pressed: " + event.getCode());

    if (event.getCode() == KeyCode.ESCAPE) {
        stage.close();
    }
});
Keyboard handler methods
MethodMeaning
setOnKeyPressedPhysical key goes down
setOnKeyReleasedPhysical key goes up
setOnKeyTypedA character is produced
KeyboardEventDemo.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class KeyboardEventDemo extends Application {
    @Override
    public void start(Stage stage) {
        Label status = new Label("Press a key");
        Scene scene = new Scene(new StackPane(status), 360, 180);
        scene.setOnKeyPressed(e -> {
            status.setText("Pressed: " + e.getCode());
            if (e.getCode() == KeyCode.ESCAPE) stage.close();
        });
        stage.setScene(scene);
        stage.setTitle("Keyboard Event Demo");
        stage.show();
    }
    public static void main(String[] args) { launch(args); }
}
Expected result
Pressing A displays:
Pressed: A

Pressing Escape closes the window.
Check your understanding: Why might a Scene key handler receive nothing while the window is open?

The intended node may not have focus, or another control may consume the event.

Step 13 of 13

5.13 Mouse Events

Learning objective

Handle mouse actions and combine multiple event types in one application.

IntegratedEventDemo.java
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class IntegratedEventDemo extends Application {
    @Override
    public void start(Stage stage) {
        TextField name = new TextField();
        name.setPromptText("Enter name and press Enter");

        CheckBox emailUpdates = new CheckBox("Email updates");
        ComboBox<String> branch = new ComboBox<>();
        branch.getItems().addAll("CSE", "IT", "EC");
        branch.setPromptText("Select branch");

        Label status = new Label("Interact with the controls");
        Button submit = new Button("Submit");

        name.setOnAction(e ->
                status.setText("Hello " + name.getText()));
        emailUpdates.setOnAction(e ->
                status.setText("Email updates: "
                        + emailUpdates.isSelected()));
        branch.setOnAction(e ->
                status.setText("Branch: " + branch.getValue()));
        submit.setOnAction(e ->
                status.setText("Submitted for " + name.getText()));

        VBox root = new VBox(12,
                name, emailUpdates, branch, submit, status);
        root.setPadding(new Insets(20));
        root.setOnMouseClicked(e ->
                status.setText("Mouse: " + (int)e.getX()
                        + ", " + (int)e.getY()));

        Scene scene = new Scene(root, 430, 300);
        scene.setOnKeyPressed(e -> {
            if (e.getCode() == KeyCode.ESCAPE) stage.close();
        });

        stage.setScene(scene);
        stage.setTitle("Integrated Event Demo");
        stage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}
Expected result
The status label changes according to the latest event.

Examples:
Hello Vatsal
Email updates: true
Branch: IT
Submitted for Vatsal
Mouse: 120, 210

Pressing Escape closes the window.
Common mouse handlers
HandlerResponds to
setOnMouseClickedClick or double-click
setOnMousePressedMouse button goes down
setOnMouseReleasedMouse button goes up
setOnMouseEnteredPointer enters a node
setOnMouseExitedPointer leaves a node
setOnMouseMovedPointer moves without dragging
setOnMouseDraggedPointer moves while a button is held
MouseEventDemo.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class MouseEventDemo extends Application {
    @Override
    public void start(Stage stage) {
        Label status = new Label("Click anywhere");
        StackPane root = new StackPane(status);
        root.setOnMouseClicked(e -> status.setText(
                "Mouse: " + (int)e.getX() + ", " + (int)e.getY()
                + " | Clicks: " + e.getClickCount()));
        stage.setScene(new Scene(root, 420, 240));
        stage.setTitle("Mouse Event Demo");
        stage.show();
    }
    public static void main(String[] args) { launch(args); }
}
Expected result
Example single click:
Mouse: 120, 90 | Clicks: 1

Example double-click:
Mouse: 120, 90 | Clicks: 2
Check your understanding: Which method reports a double-click count?

MouseEvent.getClickCount(), which returns 2 for a double-click.