Javatpoint Logo
Javatpoint Logo

JavaFX Event Handling

JavaFX provides us the flexibility to create various types of applications such as Desktop Applications, Web applications and graphical applications. In the modern day applications, the users play a vital role in the proper execution of the application. The user need to interact with the application in most of the cases.

In JavaFX, An event is occurred whenever the user interacts with the application nodes. There are various sources by using which, the user can generate the event. For example, User can make the use of mouse or it can press any button on the keyboard or it can scroll any page of the application in order to generate an event. Hence,we can say that the events are basically the notifications that tell us that something has occurred at the user's end.

A perfect Application is the one which takes the least amount of time in handling the events. This part of the tutorial This part of the tutorial describes the way in which the events are handled in JavaFX.

Types of Events

In general, the events are mainly classified into the following two types.

1. Foreground Events

Foreground events are mainly occurred due to the direct interaction of the user with the GUI of the application. Such as clicking the button, pressing a key, selecting an item from the list, scrolling the page, etc.

2. Background Events

Background events doesn't require the user's interaction with the application. These events are mainly occurred to the operating system interrupts, failure, operation completion, etc.

Processing Events in JavaFX

In JavaFX, events are basically used to notify the application about the actions taken by the user. JavaFX provides the mechanism to capture the events, route the event to its target and letting the application handle the events.

JavaFX provides the class javafx.event.Event which contains all the subclasses representing the types of Events that can be generated in JavaFX. Any event is an instance of the class Event or any of its subclasses.

There are various events in JavaFX i.e. MouseEvent, KeyEvent, ScrollEvent, DragEvent, etc. We can also define our own event by inheriting the class javafx.event.Event.

The properties of an event is described in the following table.

SN Property Description
1 Event Type It is the type of the event that is being generated. It is basically the instance of EventType class. It is hierarchical. The instance of EventType class is further classified into various type of events for example KeyEvent class contains KEY_PRESSED, KEY_RELEASED, and KEY_TYPED types.
2 Source It represents source of the event i.e. the origin which is responsible to generate the event.
3 Target It is the node on which the event is generated. It remains unchanged for the generated event. It is the instance of any of the class that implements the EventTarget interface.

Event Delivery Process

The following steps need to be followed in order to handle the events.

1. Route Construction

An Event Dispatch Chain is created in order to determine the default route of the event, whenever it is generated. The Event dispatch chain contains the path from the stage to the Node on which the event is generated.

An event dispatch chain is created in the following image for the event generated on one of the scene graph node.


JavaFX Event Handling

2. Event Capturing Phase

Once the Event Dispatch Chain is created, the event is dispatched from the source node of the event. All the nodes are traversed by the event from top to bottom. If the event filter is registered with any of these nodes, then it will be executed. If any of the nodes are not registered with the event filter then the event is transferred to the target node. The target node processes the event in that case.

3. Event Bubbling

Once the event is processed by the target node or by any of the registered filter, the event traverses all the nodes again from the bottom to the stage node. If any of these nodes are registered with the event filter, then it will get executed otherwise the process gets completed.

4. Event Handlers and Filters

Event Handlers and filters contains application logic to process an event. A node can be registered to more than one Event Filters. The interface javafx.event.EventHandler must be implemented by all the event handlers and filters.






Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


B.Tech / MCA