Kotlinlearncs.online LogoJava

    Event Driven Programming

    Writing GUI applications requires that program control be driven by the user’s interaction with the GUI. When the user moves the mouse, clicks on a button, or selects an item from a menu an “event” occurs.

    JavaFX Events
    JavaFX Events

    The javafx.event package provides the basic framework for FX events. The Event class serves as the base class for JavaFX events. Associated with each event is an event source, an event target, and an event type.

    The operating system or windowing system captures generated events and notifies the listener objects when a type of event they are listening for has occurred. The operating system notifies the listener object by sending a message to (calling) the event handling method. If no listener object is subscribed to listen for the event that occurred, the event is ignored.

    Types of Events
    Types of Events

    There are many subclasses of the Event class:

    Event Handling in JavaFX
    Event Handling in JavaFX

    Here we will just consider ActionEvents. Other types of events are handled in a similar way. When a Button is pressed, it generates an ActionEvent. If I want my program to respond to a button click, I need to tell the button what code should run.

    We need to pass an object to setOnAction() that implements the EventHandler interface. The EventHandler interface is a functional interface with one method: void handle(T event).

    Implementation as an Inner Class
    Implementation as an Inner Class

    It seems silly to need a separate class for ever event we want to handle. Also, if we want to access private attributes of the GUI, we can’t do that if we are in a different class. Solution: implement the interface as an inner class.

    Solve: JavaFX Inner Class (Practice)

    Created By: Chris Taylor
    / Version: 2023.8.0

    Implement a class, Question that contains a start(Stage stage) method. In the start() method, you must create a Button object and set the onAction event to an object from an inner class of the Question class. The inner class must implement the EventHandler<ActionEvent> interface causing "clicked" to be printed to the console whenever the event is triggered.

    Implementation as an Anonymous Inner Class
    Implementation as an Anonymous Inner Class

    Since we will only create one instance of this handler class, we could declare it as an anonymous inner class.

    Solve: JavaFX Anonymous Inner Class (Practice)

    Created By: Chris Taylor
    / Version: 2023.8.0

    Create an EventHandler<ActionEvent> reference called handler and assign it an object from an anonymous inner class that implements the EventHandler<ActionEvent> interface. The handler must display "clicked" to the console whenever triggered.

    Lambda Expression
    Lambda Expression

    Functional interfaces can be implemented with a lambda expression which have a more concise syntax.

    Solve: JavaFX Lambda (Practice)

    Created By: Chris Taylor
    / Version: 2023.8.0

    Create an EventHandler<ActionEvent> reference called handler and assign it to a lambda expression that causes the handler to display "clicked" to the console whenever triggered.

    Method Reference
    Method Reference

    Method references provide even more concise syntax. Here we pass a method reference to setOnAction().

    public class SimpleGUI extends Application {
    private Label label;
    public static void main(String[] args) {
    launch(args);
    }
    @Override
    public void start(Stage stage) {
    Pane root = new VBox();
    label = new Label("Here is some text that can be manipulated with the button above.");
    Button clickMe = new Button("Click Me");
    clickMe.setOnAction(this::handleClickMe);
    root.getChildren().addAll(label, clickMe);
    stage.setTitle("Simple GUI");
    stage.setScene(new Scene(root, 400, 300));
    stage.show();
    }
    private void handleClickMe(ActionEvent event) {
    if(label.getEffect()==null) {
    label.setEffect(new BoxBlur());

    More Practice

    Need more practice? Head over to the practice page.