Java Reference
In-Depth Information
T
ABLE
15.1
User Action, Source Object, Event Type, Handler Interface, and Handler
User Action
Source Object
Event Type Fired
Event Registration Method
Click a button
Button
ActionEvent
setOnAction(EventHandler<ActionEvent>)
Press Enter in a text field
TextField
ActionEvent
setOnAction(EventHandler<ActionEvent>)
Check or uncheck
RadioButton
ActionEvent
setOnAction(EventHandler<ActionEvent>)
Check or uncheck
CheckBox
ActionEvent
setOnAction(EventHandler<ActionEvent>)
Select a new item
ComboBox
ActionEvent
setOnAction(EventHandler<ActionEvent>)
Mouse pressed
Node, Scene
MouseEvent
setOnMousePressed(EventHandler<MouseEvent>)
Mouse released
setOnMouseReleased(EventHandler<MouseEvent>)
Mouse clicked
setOnMouseClicked(EventHandler<MouseEvent>)
Mouse entered
setOnMouseEntered(EventHandler<MouseEvent>)
Mouse exited
setOnMouseExited(EventHandler<MouseEvent>)
Mouse moved
setOnMouseMoved(EventHandler<MouseEvent>)
Mouse dragged
setOnMouseDragged(EventHandler<MouseEvent>)
Key pressed
Node, Scene
KeyEvent
setOnKeyPressed(EventHandler<KeyEvent>)
Key released
setOnKeyReleased(EventHandler<KeyEvent>)
Key typed
setOnKeyTyped(EventHandler<KeyEvent>)
15.1
What is an event source object? What is an event object? Describe the relationship
between an event source object and an event object.
15.2
Can a button fire a
MouseEvent
? Can a button fire a
KeyEvent
? Can a button fire
an
ActionEvent
?
✓
✓
Check
Point
A handler is an object that must be registered with an event source object, and it must
be an instance of an appropriate event-handling interface.
Key
Point
event delegation
event handler
Java uses a delegation-based model for event handling: a source object fires an event, and an
object interested in the event handles it. The latter object is called an
event handler
or an event
listener
. For an object to be a handler for an event on a source object, two things are needed,
as shown in Figure 15.5.
1.
The handler object must be an instance of the corresponding event-handler interface
to
ensure that the handler has the correct method for processing the event. JavaFX defines
a unified handler interface
EventHandler<T extends Event>
for an event
T
. The
handler interface contains the
handle(T e)
method for processing the event. For
example, the handler interface for
ActionEvent
is
EventHandler<ActionEvent>
;
each handler for
ActionEvent
should implement the
handle(ActionEvent e)
method for processing an
ActionEvent
.
event-handler interface
EventHandler<T extends
Event>
event handler
2.
The handler object must be registered by the source object
. 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
.
register handler
Let's revisit Listing 15.1, HandleEvent.java. Since a
Button
object fires
ActionEvent
, a
handler object for
ActionEvent
must be an instance of
EventHandler<ActionEvent>
, so
Search WWH ::
Custom Search