Java Reference
In-Depth Information
Display 17.18
Some Methods in the Class
JTextComponent
All these methods are inherited by the classes
JTextField
and
JTextArea
.
The abstract class
JTextComponent
is in the package
javax.swing.text
. The classes
JTextField
and
JTextArea
are in the package
javax.swing
.
public
String getText()
Returns the text that is displayed by this text component.
public boolean
isEditable()
Returns
true
if the user can write in this text component. Returns
false
if the user is not allowed to
write in this text component.
public void
setBackground(Color theColor)
Sets the background color of this text component.
public void
setEditable(
boolean
argument)
If
argument
is
true
, then the user is allowed to write in the text component. If
argument
is
false
,
then the user is not allowed to write in the text component.
public
void
setText(String text)
Sets the text that is displayed by this text component to be the specified
text
.
The Classes
JTextField
and
JTextArea
The classes
JTextField
and
JTextArea
can be used to add areas for changeable text to a
GUI. An object of the class
JTextField
has one line that displays some specified number of
characters. An object of the class
JTextArea
has a size consisting of a specified number of
lines and a specified number of characters per line. More text can be typed into a
JText-
Field
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);
(continued)