Java Reference
In-Depth Information
a
JTextField
or
JTextArea
than is specified in its size, but the extra text may not
be visible.
The number of characters per line and the number of lines are a guaranteed minimum.
More lines and especially more characters per line may be visible. (The space per line is
actually guaranteed to be
Characters_Per_Line
times the space for one uppercase letter M.)
SYNTAX
JTextField
Name_of_Text_Field
=
new
JTextField(
Characters_Per_Line
);
JTextArea
Name_of_Text_Area
=
new
JTextArea(
Number_of_Lines
,
Characters_Per_Line
);
EXAMPLES
JTextField name =
new
JTextField(30);
JTextArea someText =
new
JTextArea(10, 30);
There are also constructors that take an additional
String
argument that specifies an initial
string to display in the text component.
SYNTAX
JTextField
Name_of_Text_Field
=
new
JTextField(
Initial_String
,
Characters_Per_Line
)
;
JTextArea
Name_of_Text_Area
=
new
JTextArea(
Initial_String
,
Number_of_Lines
,
Characters_Per_Line
)
;
EXAMPLES
JTextField name =
new
JTextField("Enter name here.", 30);
JTextArea someText =
new
JTextArea("Enter story here.\nClick button.", 10, 30);
Number of Characters per Line
The number of characters per line (given as an argument to constructors for
JTextField
or
JTextArea
) is not the number of just any characters. The number gives the number of
em spaces in the line. An
em space
is the space needed to hold one uppercase letter M,
which is the widest letter in the alphabet. So a line that is specified to hold 20 characters
will always be able to hold at least 20 characters and will almost always hold more than
20 characters.
Scroll Bars
Scroll bars for text areas and text fields are discussed in Chapter 18. They are a nice touch,
but until you reach Chapter 18, your GUI programs will work fine without them.