JavaFX Tutorial - JavaFX Event Filters








Event filters allows us to handle an event during the event capturing phase.

A node can have one or more filters for handling an event.

We can use one filter for more than one node with more than one event type.

Event filters from the parent node take care of its child nodes and can intercept an event and prevent child nodes from acting on the event.

Registering Event Filter

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

// Register an event filter for a single node and a specific event type
scene.addEventFilter(MouseEvent.MOUSE_CLICKED,
        new EventHandler<MouseEvent>() {
          public void handle(MouseEvent e) {
            System.out.println("mouse clicked");
          };
});

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.MouseEvent;
import javafx.stage.Stage;
//from   w  ww  .  ja  va 2 s  .  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");

    // Register an event filter for a single node and a specific event type
    scene.addEventFilter(MouseEvent.MOUSE_CLICKED,
        new EventHandler<MouseEvent>() {
          public void handle(MouseEvent e) {
            System.out.println("mouse clicked");
          };
        });

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

The code above generates the following result.

null




Fitler object

The following code defines a filter object and reuse it for different event filers.

// Define an event filter
EventHandler filter = new EventHandler<InputEvent>() {
  public void handle(InputEvent event) {
    System.out.println("Filtering out event " + event.getEventType());
    event.consume();
  }
};
// Register the same filter for two different nodes
scene.addEventFilter(MouseEvent.MOUSE_PRESSED, filter);

// Register the filter for another event type
scene.addEventFilter(KeyEvent.KEY_PRESSED, filter);


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.InputEvent;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
/* ww  w  .ja  v a  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 filter
    EventHandler filter = new EventHandler<InputEvent>() {
      public void handle(InputEvent event) {
        System.out.println("Filtering out event " + event.getEventType());
        event.consume();
      }
    };
    // Register the same filter for two different nodes
    scene.addEventFilter(MouseEvent.MOUSE_PRESSED, filter);

    // Register the filter for another event type
    scene.addEventFilter(KeyEvent.KEY_PRESSED, filter);

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

The code above generates the following result.

null




Removing Event Filter

To remove an event filter, use the removeEventFilter() method. This method takes the event type and the filter as arguments.

The following code removes fitler for the MouseEvent.MOUSE_PRESSED event.

// Remove an event filter
myNode1.removeEventFilter(MouseEvent.MOUSE_PRESSED, filter);

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.InputEvent;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
// ww w.  ja  v a 2 s.co 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 filter
    EventHandler filter = new EventHandler<InputEvent>() {
      public void handle(InputEvent event) {
        System.out.println("Filtering out event " + event.getEventType());
        event.consume();
      }
    };
    // Register the same filter for two different nodes
    scene.addEventFilter(MouseEvent.MOUSE_PRESSED, filter);

    // Register the filter for another event type
    scene.addEventFilter(KeyEvent.KEY_PRESSED, filter);

    scene.removeEventFilter(MouseEvent.MOUSE_PRESSED, filter);
    
    
    root.getChildren().add(textBox);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

The code above generates the following result.

null