The java.awt.TextField
class provides a widget for
editing a single line of text. It's useful for simple input and
output.
There are four constructors:
public TextField()
public TextField(String text)
public TextField(int length)
public TextField(String text, int length)
Because of the way Java lays out text, you should not use the
noargs constructor. Either start off with a String
or
specify the number of characters this box is expected to hold. For
example,
TextField name = new TextField("Type your name here");
TextField socialSecurity = new TextField(11);
When the user hits the return or enter key inside a
TextField
, an ActionEvent
is fired. You can
trap this event with an ActionListener
object, just
like you did with buttons. However, many users do not realize that
something will happen when they hit return inside a
TextField
. Therefore, you should always provide an alternate
method to fire the action such as a button or a menu item.
The getText()
method returns the contents of the
TextField
. The setText(String s)
method changes
it.
The setEditable()
method lets you determine whether
or not, the user can modify the contents of a
TextField
.