JavaFX Tutorial - JavaFX Event Handlers








Event handlers allows to handle events during the event bubbling phase.

Registering Event Handler

An event handler is an implementation of the EventHandler interface. To register a handler, use the addEventHandler() method.

scene.addEventHandler(DragEvent.DRAG_ENTERED,
        new EventHandler<DragEvent>() {
          public void handle(DragEvent e) {
            System.out.println("drag enter");
          }
});

Full source code

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.input.DragEvent;
import javafx.stage.Stage;
/*from  w w w. j  a v a2  s.com*/
public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    Group root = new Group();
    Scene scene = new Scene(root, 300, 250);

    TextField textBox = new TextField();
    textBox.setPromptText("Write here");

    // Register an event handler for a single node and a specific event type
    scene.addEventHandler(DragEvent.DRAG_ENTERED,
        new EventHandler<DragEvent>() {
          public void handle(DragEvent e) {
            System.out.println("drag enter");
          }
        });

    root.getChildren().add(textBox);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

The code above generates the following result.

null




Reuse Event Handler

The following code defines an event handler object and reuse it to add event handler.

    // Define an event handler
    EventHandler handler = new EventHandler<InputEvent>() {
      public void handle(InputEvent event) {
        System.out.println("Handling event " + event.getEventType());
        event.consume();
      }
    };
    scene.addEventHandler(DragEvent.DRAG_EXITED, handler);

    scene.addEventHandler(MouseEvent.MOUSE_DRAGGED, handler);

Full source code

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.input.DragEvent;
import javafx.scene.input.InputEvent;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
/*ww w  .  ja va 2s  .c o  m*/
public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    Group root = new Group();
    Scene scene = new Scene(root, 300, 250);

    TextField textBox = new TextField();
    textBox.setPromptText("Write here");

    // Define an event handler
    EventHandler handler = new EventHandler<InputEvent>() {
      public void handle(InputEvent event) {
        System.out.println("Handling event " + event.getEventType());
        event.consume();
      }
    };
    scene.addEventHandler(DragEvent.DRAG_EXITED, handler);

    scene.addEventHandler(MouseEvent.MOUSE_DRAGGED, handler);

    root.getChildren().add(textBox);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

The code above generates the following result.

null




Removing Event Handler

To remove an event handler

myNode.removeEventHandler(DragEvent.DRAG_EXITED, handler);

To remove an event handler registered by a convenience method, pass null to the convenience method

node.setOnMouseDragged(null);