JavaFX Event Handler

0

JavaFX Event Handler Tutorial:

  • The JavaFX Event Handler interface allow us to handle events in bubbling Phase(A Phase of event processing).
  • A node can register more than one handler for a single event.
  • A single handler can be used for more than one node and more than one event type.
  • Event Handler is an implementation of the EventHandler interface.

Event Handling :

Event handling is provided by event filters and event handlers, which are implementations of the EventHandler interface.

handle() method :

It is a method of EventHandler interface.

We write the code inside this method which code we want to execute for an event.

Means it is a method which is executed when an event handled.


public void handle(event){
//your code
}

We add handler to a node using addEventHandler() method. AS

node.addEventHandler(event-type,handler){
//your code
};

It takes two arguments first is event type and second is the handler.

Create a handler :

EventHandler handler = new EventHandler();

It creates a new handler eventHandler().

node.addEventHandler(EventType.ROOT,handler);
It adds the handler to the node.

And at last, write the handle() method in the handler AS

Public void handle(Event event){
System.out.println("This is button");
}

so complete code for the new handler

EventHandler handler = new EventHandler(){
Public void handle(Event event){
System.out.println("This is button");
}
};
node.addEventHandler(EventType.ROOT,handler);

Example of event handler:


package javafxtuts;
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 *
 * @author JavaFXtuts.com
 */
public class Javafxtuts extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        Button btn1 = new Button();
        btn.setText("click button");
        btn1.setText("My button");
        EventHandler handler=new EventHandler(){
            //override method of EventHandler
            //this method set the task when an event is occur
            @Override
            public void handle(Event event) {
                
                System.out.println("Done.");
            
            }
        };
        
        //add event handler to buttons as
        btn.addEventHandler(EventType.ROOT, handler);
        btn1.addEventHandler(EventType.ROOT, handler);
        
        HBox root = new HBox();
        //to set the position of hbox(root) in center
        root.setAlignment(Pos.CENTER);
        root.getChildren().addAll(btn,btn1);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("javafxtuts.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}

Output:

Explanation :
  • First, we build an EventHandler As explained above.
  • In the above example, we just want an output “Done” when an event occurs.It is done by applying println() method in the handle() method of EventHandler As
    
    	public void handle(Event event) {
                     System.out.println("Done.");
                }
                

    And when an event occurs then this method is called and it give an output “Done”.

  • now to add or register This EventHandler to the button we do As
    btn.addEventHandler(EventType.ROOT, handler);
    btn1.addEventHandler(EventType.ROOT, handler);
  • And finally, we put these button in HBox layout.
Share.

Leave A Reply