Java Reference
In-Depth Information
members,
inner classes
can be declared
public
,
protected
or
private
. Since event han-
dlers tend to be specific to the application in which they're defined, they're often imple-
mented as
private
inner classes or as
anonymous inner classes
(Section 12.11).
GUI components can generate many events in response to user interactions. Each
event is represented by a class and can be processed only by the appropriate type of event
handler. Normally, a component's supported events are described in the Java API docu-
mentation for that component's class and its superclasses. When the user presses
Enter
in
a
JTextField
or
JPasswordField
, an
ActionEvent
(package
java.awt.event
) occurs.
Such an event is processed by an object that implements the interface
ActionListener
(package
java.awt.event
). The information discussed here is available in the Java API
documentation for classes
JTextField
and
ActionEvent
. Since
JPasswordField
is a sub-
class of
JTextField
,
JPasswordField
supports the same events.
To prepare to handle the events in this example, inner class
TextFieldHandler
implements interface
ActionListener
and declares the only method in that interface—
actionPerformed
(lines 53-80). This method specifies the tasks to perform when an
ActionEvent
occurs. So, inner class
TextFieldHandler
satisfies
Step 1
listed earlier in this
section. We'll discuss the details of method
actionPerformed
shortly.
Registering the Event Handler for Each Text Field
In the
TextFieldFrame
constructor, line 42 creates a
TextFieldHandler
object and as-
signs it to variable
handler
. This object's
actionPerformed
method will be called auto-
matically when the user presses
Enter
in any of the GUI's text fields. However, before this
can occur, the program must register this object as the event handler for each text field.
Lines 43-46 are the
event-registration
statements that specify
handler
as the event handler
for the three
JTextField
s and the
JPasswordField
. The application calls
JTextField
method
addActionListener
to register the event handler for each component. This meth-
od receives as its argument an
ActionListener
object, which can be an object of any class
that implements
ActionListener
. The object
handler
is an
ActionListener
, because
class
TextFieldHandler
implements
ActionListener
. After lines 43-46 execute, the ob-
ject
handler
listens for events
. Now, when the user presses
Enter
in any of these four text
fields, method
actionPerformed
(line 53-80) in class
TextFieldHandler
is called to han-
dle the event. If an event handler is
not
registered for a particular text field, the event that
occurs when the user presses
Enter
in that text field is
consumed
—i.e., it's simply
ignored
by the application.
Software Engineering Observation 12.1
The event listener for an event must implement the appropriate event-listener interface.
Common Programming Error 12.2
If you forget to register an event-handler object for a particular GUI component's event
type, events of that type will be ignored.
Details of Class
TextFieldHandler
's
actionPerformed
Method
In this example, we use one event-handling object's
actionPerformed
method (lines 53-80)
to handle the events generated by four text fields. Since we'd like to output the name of each
text field's instance variable for demonstration purposes, we must determine
which
text field