Chapter 8 |
Introduction
Last chapter's material focuses on the basics elements of JavaFX programs. Nevertheless, one important part is left out there - event handling. Most GUI controls generate events and channel the program flow.
An event can be defined as a signal to the program that something has happened. Events are triggered by external user actions, such as mouse movements, mouse clicks, and keystrokes. The program can be programmed to respond or ignore the triggered event.
In JavaFX, any event is an instance of class javafx.event.Event
or any subclass of Event
. Every event includes information about the event type, the event source and the event target. Therefore, the events can be divided into DragEvent
, KeyEvent
, TouchEvent
etc.
Each event type can further be classified within its class. For example, the KeyEvent
class contains the following event types: KEY_PRESSED
, KEY_RELEASED
, KEY_TYPED
. In the subtypes, the event type ANY is used to mean any event type in the present event class. For example, to provide the same response to any type of a key event, use KeyEvent.ANY
as the event type. To respond only when a key is released, use the KeyEvent.KEY_RELEASED
event type.
A component that creates an event and fires it is called the event source object, or simply source object or source component. For example, a button is the source object for the buttonclicking action event. To respond to a button click, we need to write a piece of code that processes the button-clicking action. We need to create an object which is capable of handling the action event on a button. This object is called an event handler.
Used resources:
Chapter 8 |