View RSS Feed

A JavaFX Blog

Reacting to User Input in a JavaFX Application with Event Handlers

Rate this Entry
by , 12-11-2016 at 06:33 PM (2280 Views)
In a GUI application environment, events are generated every time a user interacts with the application using the input devices of the computer where the application is running; typical examples of events are a click with the mouse or a key press on the keyboard. In JavaFX, events are represented by the javafx.event.Event class; this is a subclass of java.util.EventObject, which is the base class representing a generic event in Java. The EventObject class offers a basic functionality related events, which is the ability to associate an event to its source, i.e. the object on which the event occurred. To retrieve the source of an event, the Object getSource() method of the EventObject class is used.

In a JavaFX application, events are handled by objects implementing the javafx.event.EventHandler<T extends Event> interface. As can be seen from its definition, this interface represents an event handler for a specific subclass of Event, so for example a handler for mouse events would implement the EventHandler<MouseEvent> interface. The only method to be implemented by an event handler is void handle(T event), which takes as argument the event to be handled (note that the class of this argument corresponds to the event subclass for which an event handler implementation is defined). Typically, event handlers are defined in an application via anonymous inner classes. In JavaFX, events are processed (i.e., the handle() method of event handlers is called) from within the application thread, that is the thread managing the GUI of the application, so as a general rule the execution of the handle() method should be fast, to keep the GUI responsive.

The following example demonstrates an event handler associated to a button on the user interface:

Java Code:
public class EventExample extends Application {

  public void start(Stage primaryStage) {

    primaryStage.setTitle("Event Example");
    FlowPane rootNode = new FlowPane();
    final Button button = new Button("my button");
    button.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent event) {
        button.setText("pressed");
      }
    });

    rootNode.getChildren().add(button);
    Scene scene = new Scene(rootNode);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}
In this example, the event handler is defined as an implementation of the EventHandler<ActionEvent> interface (ActionEvent is an Event subclass representing some type of action, and in this context represents a click on the button), and is associated to the button by calling setOnAction(). The handle() method of the event handler simply changes the text displayed on the button itself, to provide feedback that the button has been pressed. When launching this application, its user interface appears as in the following screenshot:

Name:  Reacting to User Input in a JavaFX Application with Event Handlers-1.png
Views: 862
Size:  3.5 KB

After clicking on the button, the text on the button changes as below:

Name:  Reacting to User Input in a JavaFX Application with Event Handlers-2.png
Views: 844
Size:  3.4 KB

Note: This example has been written using JavaFX 8.

Submit "Reacting to User Input in a JavaFX Application with Event Handlers" to Facebook Submit "Reacting to User Input in a JavaFX Application with Event Handlers" to Digg Submit "Reacting to User Input in a JavaFX Application with Event Handlers" to del.icio.us Submit "Reacting to User Input in a JavaFX Application with Event Handlers" to StumbleUpon Submit "Reacting to User Input in a JavaFX Application with Event Handlers" to Google