Object Oriented Programming: Unit V: JAVAFX Event Handling, Controls and Components

JAVAFX Event Basics

Syntax with Example Java Programs

Event means any activity that interrupts the current ongoing activity. For example: when user clicks button then it generates an event. To respond to button click we need to write the code to process the button clicking action.

Event Basics

• Event means any activity that interrupts the current ongoing activity. For example: when user clicks button then it generates an event. To respond to button click we need to write the code to process the button clicking action.

Event Source Object : The object that generates the event is called event source object. For example- if the event gets generated on clicking the button, then button is the event source object.

Event Handler: The event handling code written to process the generated event is called event handler.

Event Listener: The task of handling an event is carried out by event listener. When an event occurs, first of all an event object of the appropriate type is created. This object is then passed to a Listener. A listener must implement the interface that has the method for event handling.

Registering Handlers and Handling Events

•  JavaFX has just one interface for all kinds of Event Handlers.

• It is an instance of the EventHandler<T extends Event> interface. This interface defines the common behavior for all handlers. <T extends Event> denotes that T is a generic type that is a subtype of Event.

• The EventHandler object handler must be registered with the event source object using the method source.setOnAction(handler).

• The EventHandler<ActionEvent> interface contains the handle(ActionEvent) method for abc processing the action event. We have to write the handler class that must override this method to respond to the event. Hence we must import javafx.event.EventHandler class in our application program.

• The event Class hierarchy is as shown below -

• There are user actions that are associated with some source objects. For these actions particular event is fired. The JavaFX associate some setOnXXX methods for registering these events. The list of such user actions, source objects and setOnXXX methods are enlisted in the following table.

• Java makes use of delegation based model for event handling and registration. This model is as shown below.


The event handling process is a two step process. It performs following functionalities –

 (1) The handler object is an instance of appropriate EventHandler interface. The EventHandler interface is defined as EventHandler<T extends Event>. It contains handle() function for processing the event.

(2) The handler object is registered by the source object. The registration methods depend on the event type. For ActionEvent, the method is setOnAction. For a mouse pressed event, the method is setOnMousePressed. For a key pressed event, the method is setOnKeyPressed.

For example

Button button = new Button("Click Me");

//Event object is button

button.setOnAction(new EventHandler <ActionEvent>() //Event registration

{

public void handle (ActionEvent event)

………

……...

}

Following is a complete JavaFX program, that displays the message on clicking the button. Thus button click event is handled in the following program.

Programming Example

package myjavafxapplication;

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.layout.HBox;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx. stage. Stage;

public class MyJavaFXApplication extends Application {

@Override

public void start(Stage primaryStage) {

HBox root = new HBox();

Button button = new Button("Click Me");

button.setOnAction(new EventHandler <ActionEvent>()

{

public void handle (ActionEvent event)

{

System.out.println("Button is clicked!!");

}

}

};

root.getChildren().add(button);

Scene scene = new Scene(root,300,200);

primaryStage.setScene(scene);

primaryStage.setTitle("Event Handling Demo");

primaryStage.show();

}

public static void main(String[] args) {

launch(args);

}

}

Output

On clicking the above button, we get following message in the output window



Object Oriented Programming: Unit V: JAVAFX Event Handling, Controls and Components : Tag: : Syntax with Example Java Programs - JAVAFX Event Basics