Java Reference
In-Depth Information
eral other capabilities that you might want to explore, such as cut, paste, and append. You
can also select a portion of the text under program control.
One especially useful
TextField
option is the ability to set a prompting message inside
the text field when the user attempts to use a blank field. To do this, call
setPromptText(
)
, shown here:
final void setPromptText(String
str
)
In this case,
str
is the string displayed in the text field when no text has been entered. It is
displayed using low-intensity (such as a gray tone).
When the user presses
ENTER
while inside a
TextField
, an action event is generated. Al-
though handling this event is often helpful, in some cases, your program will simply obtain
the text when it is needed, rather than handling action events. Both approaches are demon-
strated by the following program. It creates a text field that requests a name. When the
user presses
ENTER
while the text field has input focus, or presses the Get Name button, the
string is obtained and displayed. Notice that a prompting message is also included.