Java Reference
In-Depth Information
Display 17.17
A Text Field
(part 3 of 3)
R
ESULTING
GUI
(When program is started and a name entered)
R
ESULTING
GUI
(After clicking the "Click me" button)
The following line from the method
actionPerformed
in Display 17.17 uses both
getText
and
setText
:
name.setText("Hello" + name.getText());
This line changes the string in the text field
name
to
"Hello"
followed by the old string
value in the text field. The net effect is to insert the string
"Hello"
in front of the string
displayed in the text field.
getText
and
setText
The classes
JTextField
and
JTextArea
both contain methods called
getText
and
setText
.
The method
getText
can be used to retrieve the text written in the text field or text area. The
method
setText
can be used to change the text written in the text field or text area.
SYNTAX
Name_of_Text_Component
.getText()
returns the text currently displayed in the text
field or text area.
Name_of_Text_Component
.setText
(
New_String_To_Display
)
;