Java Reference
In-Depth Information
and
JTextArea
classes. If
theText
names an object in either of the classes
JTextField
or
JTextArea
, then the following
theText.setEditable(
false
);
will set
theText
so that only your GUI program can change the text in the text
component
theText
; the user cannot change the text. After this invocation of
setEditable
, if the user clicks the mouse in the text component named
theText
and
then types at the keyboard, the text in the text component will not change.
To reverse things and make
theText
so that the user can change the text in the text
component, use
true
in place of
false
, as follows:
theText.setEditable(
true
);
If no invocation of
setEditable
is made, then the default state allows the user to
change the text in the text component.
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