Java Reference
In-Depth Information
Creating the GUI
Line 22 sets the
TextFieldFrame
's layout to
FlowLayout
. Line 25 creates
textField1
with
10
columns of text. A text column's width in
pixels
is determined by the average width of
a character in the text field's current font. When text is displayed in a text field and the
text is wider than the field itself, a portion of the text at the right side is not visible. If you're
typing in a text field and the cursor reaches the right edge, the text at the left edge is pushed
off the left side of the field and is no longer visible. Users can use the left and right arrow
keys to move through the complete text. Line 26 adds
textField1
to the
JFrame
.
Line 29 creates
textField2
with the initial text
"Enter
text
here"
to display in the
text field. The width of the field is determined by the width of the default text specified in
the constructor. Line 30 adds
textField2
to the
JFrame
.
Line 33 creates
textField3
and calls the
JTextField
constructor with two argu-
ments—the default text
"Uneditable
text
field"
to display and the text field's width in
columns (
21
). Line 34 uses method
setEditable
(inherited by
JTextField
from class
JTextComponent
) to make the text field
uneditable
—i.e., the user cannot modify the text
in the field. Line 35 adds
textField3
to the
JFrame
.
Line 38 creates
passwordField
with the text
"Hidden
text"
to display in the text
field. The width of the field is determined by the width of the default text. When you exe-
cute the application, notice that the text is displayed as a string of asterisks. Line 39 adds
passwordField
to the
JFrame
.
Steps Required to Set Up Event Handling for a GUI Component
This example should display a message dialog containing the text from a text field when
the user presses
Enter
in that text field. Before an application can respond to an event for
a particular GUI component, you must:
1.
Create a class that represents the event handler and implements an appropriate
interface—known as an
event-listener interface
.
2.
Indicate that an object of the class from
Step 1
should be notified when the event
occurs—known as
registering the event handler
.
Using a Nested Class to Implement an Event Handler
All the classes discussed so far were so-called
top-level classes
—that is, they
were
not de-
clared
inside
another class. Java allows you to declare classes
inside
other classes—these are
called
nested classes
. Nested classes can be
static
or non-
static
. Non-
static
nested
classes are called
inner classes
and are frequently used to implement
event handlers
.
An inner-class object must be created by an object of the top-level class that contains
the inner class. Each inner-class object
implicitly
has a reference to an object of its top-level
class. The inner-class object is allowed to use this implicit reference to directly access all
the variables and methods of the top-level class. A nested class that's
static
does not
require an object of its top-level class and does not implicitly have a reference to an object
of the top-level class. As you'll see in Chapter 13, Graphics and Java 2D, the Java 2D
graphics API uses
static
nested classes extensively.
Inner Class
TextFieldHandler
The event handling in this example is performed by an object of the
private
inner class
TextFieldHandler
(lines 50-81). This class is
private
because it will be used only to cre-
ate event handlers for the text fields in top-level class
TextFieldFrame
. As with other class