JavaFX Event Handling - JavaFX

What is JavaFX Event Handling?

In JavaFX, we will expand GUI applications, web applications and graphical programs. In such packages, whenever a user interacts with the application (nodes), an event is said to had been occurred.

For example, clicking on a button, moving the mouse, entering a character through keyboard, choosing an object from list, scrolling the page are the activities that reasons an event to happen.

Types of Events

The events can be broadly categorized into the following categories −

  • Foreground events − those events which require the direct interaction of a consumer. they are generated as effects of a person interacting with the graphical components in a Graphical consumer Interface. for example, clicking on a button, transferring the mouse, coming into a person through keyboard, choosing an item from list, scrolling the page, etc.
  • Background Events − the ones events that require the interaction of give up user are known as history events. The operating system interruptions, hardware or software failure, timer expiry, operation of completion are the example of background events.

Events in JavaFX

JavaFX presents help to deal with a wide varieties of events. The class named event of the package javafx.event is the base class for an event.

An example of any of its subclass is an event. JavaFX presents a huge variety of events. some of them are are listed below.

  • Mouse event − this is an enter event that occurs when a mouse is clicked. it is represented through the class named MouseEvent. It includes actions like mouse clicked, mouse pressed, mouse released, mouse moved, mouse entered target, mouse exited target, etc.
  • Key event − that is an enter event that shows the key stroke occurred on a node. it is represented through the class named KeyEvent. This event consists of actions like key pressed, key released and key typed.
  • Drag event − that is an input event which happens when the mouse is dragged. it is represented through the class named DragEvent. It consists of actions like drag entered, drag dropped, drag entered target, drag exited target, drag over, etc.
  • Window event − that is an event associated with window showing/hiding moves. it is represented through the class named WindowEvent. It consists of moves like window hiding, window proven, window hidden, window showing, etc.

Event Handling

Event handling is the mechanism that controls the event and decides what need to appear, if an event occurs. This mechanism has the code that is called an event handler that is performed whilst an event occurs.

JavaFx presents handlers and filters to handle events. In JavaFX each event has −

  • Target − The node on which an event took place. A target may be a window, scene, and a node.
  • Source − The supply from which the event is generated can be the supply of the event. within the above scenario, mouse is the source of the event.
  • Type − type of the happened event; in case of mouse event – mouse pressed, mouse released are the kind of events.

Assume that we have an application which has a Circle, stop and Play Buttons inserted using a set item as follows −

JavaFX - Event Handling


In case you click on the play button, the source can be the mouse, the goal node can be the play button and the type of the event generated is the mouse click.

Phases of event handling in JavaFX

Whenever an event is generated, JavaFX undergoes the following stages.

Route Construction

Whenever an event is generated, the default/initial route of the event is determined by means of construction of an Event Dispatch Chain. it is the path from the stage to the source Node.

Following is the event dispatch chain for the event generated, when we click on at the play button inside the above scenario.

JavaFX - Event Handling


Event Capturing Phase

After the construction of the event dispatch chain, the root node of the application dispatches the event. This event travels to all nodes in the dispatch chain (from top to backside). If any of these nodes has a filter registered for the generated event, it will be done. If none of the nodes inside the dispatch chain has a filter out for the event generated, then it is exceeded to the target node and finally the goal node techniques the event.

Event Bubbling Phase

In the event bubbling section, the event is travelled from the goal node to the level node (bottom to top). If any of the nodes within the event dispatch chain has a handler registered for the generated event, it will be done. If none of those nodes have handlers to handle the event, then the event reaches the root node and finally the technique will be finished.

Event Handlers And Filters

Event filters and handlers are the ones which includes application logic to method an event. A node can sign up to more than one handler/filter. In case of parent–toddler nodes, you may offer a common filter/handler to the parents, which is processed as default for all of the child nodes.

As referred to above, throughout the event, processing is a filter out this is executed and at some point of the event bubbling phase, a handler is done. all the handlers and filters implement the interface EventHandler of the package javafx.event.

Adding and Removing Event Filter

To feature an event filter to a node, you want to sign in this filter the usage of the technique addEventFilter() of the Node class.

In the same way, you can remove a filter using the method removeEventFilter() as shown below –

Event Handling Example

Following is an example demonstrating the event handling in JavaFX using the event filters. Save this code in a file with name EventFiltersExample.java.

Collect and execute the saved java file from the command prompt using the following commands.

On executing, the above program generates a JavaFX window as shown below.

JavaFX - Event Handling

Adding and Removing Event Handlers

To add an event handler to a node, you want to register this handler using the technique addEventHandler() of the Node class as proven below.

In the same way, you can remove an event handler using the method removeEventHandler() as shown below –

Example

The following program is an example demonstrating the event handling in JavaFX the use of the event handlers.

Save this code in a file with name EventHandlersExample.java.

Collect and execute the saved java file from the command prompt using the following commands.

On executing, the above program generates a JavaFX window displaying a text field and a 3D box as shown below –

JavaFX - Event Handling

Here, if you type a letter within the text subject, the 3D box begins rotating along the x axis. if you click on at the box once more the rotation stops.

Using Convenience Methods for Event Handling

Some of the instructions in JavaFX define event handler properties. through setting the values to these properties the usage of their respective setter techniques, you may register to an event handler. these techniques are called convenience methods.

Most of those techniques exist within the classes like Node, Scene, Window, etc., and they are available to all their sub classes.

For example, to add a mouse event listener to a button, you may use the ease technique setOnMouseClicked() as shown below.

Example

The following program is an example that demonstrates the event handling in JavaFX using the ease techniques.

Save this code in a file with the name ConvinienceMethodsExample.java.

Collect and execute the saved java file from the command prompt using the following commands.

On executing, the above application generates a JavaFX window as proven below. here click on the play button to start the animation and click at the stop button to prevent the animation.

JavaFX - Event Handling

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JavaFX Topics