JavaFX Event Handling
JavaFX Event Handling with JavaFX Overview, Install Java, Install Eclipse, JavaFX with Eclipse, JavaFX Architecture, JavaFX Application Structure, First JavaFX Application etc.
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 needs to interact with the application in most cases.
In JavaFX, An event has occurred whenever the user interacts with the application nodes. There are various sources by using which, the user can generate the event. For example, Users can make the use of the 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 that takes the least amount of time in handling the events. 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 don't require the user's interaction with the application. These events have 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 are described in the following table.
|1||Event Type||It is the type of 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 types of events for example KeyEvent class contains KEY_PRESSED, KEY_RELEASED, and KEY_TYPED types.|
|2||Source||It represents a 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 nodes.
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 filters, 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 contain 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.