×

JavaFX Event Filters

In JavaFX, in order to handle the event which are created by any of the mouse action, keyboard action, rotate action, scroll action Event filters are used. The event filters comes in picture and process the particular event during the event capturing phase. Before providing the actual event handling logic event filter must be registered with particular node. The javafx provides us handle() method of the event handler interface to provide actual execution logic when the event is generated. JavaFX also provides us the facility to register one event filter on more than one node. And for more than one event types.

Adding Event filters on the node:

In order to handle any event on a particular node in a event capturing phase, the event filters must be registered with that particular node. To register particular event filter on the specified node, we use addEventFilter() method. The handle() method of this interface allows us to define logic for execution of event when a particular event is triggered. The user has to pass two arguments for the method, the first one is the type of the event to be generated and the second one is the filter which contains all the code for handling specific event.

Syntax:

node.addEventFilter (Event-Type, new EventHandler<Event_Type>(){   
public void handle(Event_Type){   
//code to handle the event
});   

Removing Event filters on the node:

When the user no longer want to handle events on a particular node, we can remove event filter associated with a particular node using removeEventFilter() method. This method takes two arguments, first one is type of the event and the second one is object of the event filter.

Syntax:

node.removeEventFilter(Event-type, eventfilter_Object);

JavaFX Event Filter for Key Events

Example:

import javafx.application.Application;  
import javafx.event.EventHandler;  
import javafx.scene.Group;  
import javafx.scene.Scene;  
import javafx.scene.control.Label;  
import javafx.scene.control.TextField;  
import javafx.scene.input.KeyEvent;  
import javafx.stage.Stage;  
public class EventUI extends Application{  
  
    @Override  
    public void start(Stage primaryStage) throws Exception {  
      
        Label l1 = new Label(" Enter any text ");  
        Label l2 = new Label(" Event filtering ");  
          
        l1.setTranslateX( 100);  
        l1.setTranslateY( 100);  
          
        l2.setTranslateX( 100);  
        l2.setTranslateY( 150);  
          
        TextField tf1 = new TextField();  
        TextField tf2 = new TextField();  
          
        tf1.setTranslateX( 250);  
        tf1.setTranslateY( 100);  
        tf2.setTranslateX( 250);  
        tf2.setTranslateY( 150);  
        tf2.setMinWidth( 200);
          
        EventHandler<KeyEvent> newfilter = new EventHandler<KeyEvent>() {  
            @Override  
            public void handle(KeyEvent event) {  
                tf2.setText("Event filter used: "+event.getEventType());  
                tf1.setText(event.getText());  
                event.consume();  
            }  
        };  
          
        tf1.addEventFilter(KeyEvent.ANY,newfilter );  
          
        Group root = new Group();  
        root.getChildren().addAll(l1, l2, tf1, tf2);  
        Scene scene = new Scene(root, 500, 300);  
        primaryStage.setScene(scene);  
        primaryStage.setTitle(" JavaFX Event Filter ");  
        primaryStage.show();  
    }  
    public static void main(String[] args) {  
        launch(args);  
    }  
  
} 

Output:

JavaFX Event Filters JavaFX Event Filters JavaFX Event Filters

Explanation:

In order to create the event on any given components in JavaFX using event filter, we have to import all the required libraries. Then we have created one class named EventUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold various components with the event filters, a Group object is created which is then passed to the Scene class object.

One label is created for asking the user to any text and the second label showing the event filter occurred on that particular node. Two text fields created using constructor. First text field accepts the user input and the second text field shows the particular key event occurred. Various setters methods are used to set positions for label and text fields. According to the key action of the user the event is handled. The handle() method with the key event object passed to it is used for the actual business logic. The text field 2 will show the event occurred. It may be the key_pressed, key_typed or key_released. The getEventType() method is used to get the type of event generated by user action using event filter.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with title “ JavaFX Event Filter”. Also, it displays One label to enter any text and other to show event filter. When user type a key, it shows key_typed else show key_pressed or key_released based on key action performed by the user.


Related Topics

JavaFX Installation

In order to run JavaFX applications, we need to have eclipse and java installed on our machine. Prerequisite: Operating systemAny Operating system (Windows, Linux, Mac)Memory requirementNo minimum requirementDisk SpaceNo minimum requirementJDK versionJDK...

3 minutes read.

JavaFX Translation

In the JavaFX application, in order to apply various Translate effect, we use this class. It has the ability to Translate the given object according to the specified parameter. All...

4 minutes read.

JavaFX TextField

In the JavaFX application, to get the user information as input from the user, a form is created using textfields which allows the user to enter input for specific fields....

4 minutes read.

JavaFX Menu

In the JavaFX application, in order to create a menu, menu items, and menu bar, Menu, MenuItem, and MenuBar class is used. The menu allows us to choose options among...

4 minutes read.

JavaFX Scale Transition

In the JavaFX application, in order to generate the scale transition, we have to apply scale transition on any particular shape. It has the ability to scale the shape along...

4 minutes read.

JavaFX Transformation

In the JavaFX application, in order to apply various transformation effects, we use Transform class. It has the ability to create the transformation on the given object. This transformation changes...

3 minutes read.

JavaFX GridPane

In the JavaFX application, in order to set Grid Pane as a layout, the GridPane class is used. The GridPane layout allows us to arrange the components in the form...

4 minutes read.

JavaFX Pie Chart

In the JavaFX application, in order to create Pie Chart, the PieChart class is used. The Pie Chart allows us to plot data along with the sectors of the circle....

3 minutes read.

JavaFX Line Chart

In the JavaFX application, in order to create a Line Chart, LineChart class is used. The LineChart allows us to plot markers point according to the entities on number axes....

4 minutes read.

JavaFX Drop Shadow

In the JavaFX application, in order to apply various Drop Shadow effect, we use this class. It has the ability to create the drop shadow of the given object. This...

4 minutes read.

JavaFX Event Handling

JavaFX gives way to handle various events in web application, desktop applications and graphical applications. In the JavaFX application, in order to allow user to interact with application various events...

4 minutes read.

JavaFX Image Input

In the JavaFX application, in order to apply various image input effect, we use the ImageInput class. It has the ability to fill the specified shape with a given image...

4 minutes read.

JavaFX Slider

In the JavaFX application, in order to slide over a range of data having values, Slider is used. It provides a pane with a Slider to move over a range...

4 minutes read.

JavaFX Scatter Chart

In the JavaFX application, in order to create Scatter Chart, the ScatterChart class is used. The Scatter Chart allows us to plot data along the x and y axis of...

4 minutes read.

JavaFX StackPane

In the JavaFX application, in order to set Stack Pane as a layout, the StackPane class is used. The StackPane layout allows us to arrange the components in the stack...

4 minutes read.

JavaFX Stroke Transition

In the JavaFX application, in order to generate the stroke transition, we have to apply stroke transition on any particular shape. It has the ability to give different color strokes...

4 minutes read.

JavaFX Translate Transition

In the JavaFX application, in order to generate the translate transition, we have to apply translate transition on any particular shape. It has the ability to translate the shape along...

3 minutes read.

JavaFX ProgressBar

In the JavaFX application, in order to show the progress of work ProgressBar is used. It shows how much work is done. In order to use ProgressBar in JavaFX, javafx.scene.control.ProgressBar...

4 minutes read.

JavaFX TilePane

In the JavaFX application, in order to set TilePane as a layout, the TilePane class is used. The TilePane layout allows us to arrange the components in the same sized...

4 minutes read.

JavaFX Circle

In the JavaFX application, in order to draw a circle, the Circle class is used. The Circle allows us to join any points in the space. All methods needed for...

4 minutes read.