"textField1: %s"
,
60
);
61
62
// user pressed Enter in JTextField textField2
63
else if
(
)
64
string = String.format(
"textField2: %s"
,
65
);
66
67
// user pressed Enter in JTextField textField3
68
else if
(
)
69
string = String.format(
"textField3: %s"
,
70
);
71
72
// user pressed Enter in JTextField passwordField
73
else if
(
)
74
string = String.format(
"passwordField: %s"
,
75
);
76
77
// display JTextField content
78
JOptionPane.showMessageDialog(
null
, string );
79
}
// end method actionPerformed
80
}
// end private inner class TextFieldHandler
81
}
// end class TextFieldFrame
Fig. 14.9
|
JTextFields
and
JPasswordFields
. (Part 2 of 2.)
// construct textfield with default text and 21 columns
textField3 =
new
JTextField(
"Uneditable text field"
,
21
);
textField3.setEditable(
false
);
// disable editing
passwordField =
new
JPasswordField(
"Hidden text"
);
TextFieldHandler handler =
new
TextFieldHandler();
textField1.addActionListener( handler );
textField2.addActionListener( handler );
textField3.addActionListener( handler );
passwordField.addActionListener( handler );
private class
TextFieldHandler
implements
ActionListener
public void
actionPerformed( ActionEvent event )
event.getSource() == textField1
event.getActionCommand()
event.getSource() == textField2
event.getActionCommand()
event.getSource() == textField3
event.getActionCommand()
event.getSource() == passwordField
event.getActionCommand()

564
Chapter 14
GUI Components: Part 1
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
.

