×

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 Sequential Transition

In the JavaFX application, in order to generate the sequential transition, we have to apply sequential transition on any particular shape. It has the ability to execute multiple transactions sequentially...

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 Blend

In the JavaFX application, in order to apply various blend effect, we use Blend class. It has the ability to blend the colors of the shape. All methods needed for...

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 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 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...

3 minutes read.

JavaFX Convenience Methods

The JavaFX allows us to use various convenience methods to handle events on various nodes in JavaFX applications. The convenience methods provides us easy and effective way to add any...

4 minutes read.

JavaFX Button Styling

 In the JavaFX application, we can apply CSS styling to buttons in various ways. We can change the size of the button as well as border colour, text colour can...

3 minutes read.

JavaFX Label

The UI Control Label in a JavaFX is used to display the simple text. To use Label in JavaFX application javafx.scene.control.The label class is used. We can place Labels on...

3 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 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 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 Light Distant

In the JavaFX application, in order to apply various Light Distant effect from a single source, we use this class. It has the ability to apply light from a distant...

4 minutes read.

JavaFX PasswordField

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

4 minutes read.

JavaFX Lighting

In the JavaFX application, in order to apply various Lighting effect from a single source, we use this class. It has the ability to lighten a node from a given...

4 minutes read.

JavaFX Color Input

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

4 minutes read.

JavaFX Arc

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

5 minutes read.

JavaFX BorderPane

In the JavaFX application, in order to set Border Pane as a layout, the BorderPane class is used. The BorderPane layout allows us to arrange the components in the left,...

4 minutes read.

JavaFX Light Point

In the JavaFX application, in order to apply various LightPoint effect from a single source, we use this class. It has the ability to apply light from a single source...

4 minutes read.

JavaFX Ellipse

In the JavaFX application, in order to draw an Ellipse, the Ellipse class is used. The Ellipse allows us to join any points in the space to draw the unique...

7 minutes read.