Java Reference
In-Depth Information
Fig. 12.10
|
Testing
TextFieldFrame
. (Part 2 of 2.)
This application used a single object of class
TextFieldHandler
as the event listener
for four text fields. Starting in Section 12.10, you'll see that it's possible to declare several
event-listener objects of the same type and register each object for a separate GUI compo-
nent's event. This technique enables us to eliminate the
if
…
else
logic used in this
example's event handler by providing separate event handlers for each component's events.
Java SE 8: Implementing Event Listeners with Lambdas
Recall that interfaces like
ActionListener
that have only one
abstract
method are func-
tional interfaces in Java SE 8. In Section 17.9, we show a more concise way to implement
such event-listener interfaces with Java SE 8 lambdas.
In Section 12.6, you learned that information about the event that occurs when the user
presses
Enter
in a text field is stored in an
ActionEvent
object. Many different types of
events can occur when the user interacts with a GUI. The event information is stored in
an object of a class that extends
AWTEvent
(from package
java.awt
). Figure 12.11 illus-
trates a hierarchy containing many event classes from the package
java.awt.event
. Some
of these are discussed in this chapter and Chapter 22. These event types are used with both