Previous | Next | Trail Map | Creating a User Interface | Using Components, the GUI Building Blocks

How to Use TextAreas and TextFields

The TextArea(in the API reference documentation) and TextField(in the API reference documentation) classes display selectable text and, optionally, allow the user to edit the text. You can subclass TextArea and TextField to perform such tasks as checking for errors in the input. As with any Component, you can specify the background and foreground colors and font used by TextAreas and TextFields. You can't, however, change their basic appearance.

Both TextArea and TextField are subclasses of TextComponent(in the API reference documentation). From TextComponent they inherit methods that allow them to set and get the current selection, enable and disable editing, get the currently selected text (or all the text), and set the text.

Below is an applet that displays first a TextField and then a TextArea. The TextField is editable; the TextArea isn't. When the user presses Return in the TextField, its contents are copied to the TextArea and then selected in the TextField.


Note: Because some old browsers don't support 1.1, the above applet is a 1.0 version (here is the 1.0 code; here's the 1.1 code). To run the 1.1 version of the applet, go to example-1dot1/TextDemo.html.

Here's the program. Here's just its code that creates, initializes, and handles events in the TextArea and TextField:

//Where instance variables are defined:
TextField textField;
TextArea textArea;

public void init() {
    textField = new TextField(20);
    textArea = new TextArea(5, 20);
    textArea.setEditable(false);

    ...//Add the two components to the panel. 
}

public boolean action(Event evt, Object arg) {
    String text = textField.getText();
    textArea.appendText(text + "\n");
    textField.selectAll();
    return true;
}
The TextComponent superclass of TextArea and TextField supplies the getText(), setText(), setEditable(), and selectAll() methods used in the above code example. It also supplies the following useful methods: getSelectedText(), isEditable(), getSelectionStart(), and getSelectionEnd(). It also provides a select() method that lets you select text between beginning and end positions that you specify.

The TextField class has four constructors: TextField(), TextField(int), TextField(String), and TextField(String, int). The integer argument specifies the number of columns in the text field. The String argument specifies the text initially displayed in the text field. The TextField class also supplies the following handy methods:

int getColumns()
Returns the number the columns in the text field.
setEchoChar()
Sets the echo character, which is useful for password fields.
char getEchoChar() boolean echoCharIsSet()
These methods let you ask about the echo character.

Like the TextField class, the TextArea class also has four constructors: TextArea(), TextArea(int, int), TextArea(String), and TextArea(String, int, int). The integer arguments specify the number of rows and columns (respectively) in the text area. The String argument specifies the text initially displayed in the text area.

The TextArea class supplies the appendText() method used in the code example above. It also supplies these methods:

int getRows(), int getColumns()
Return the number of rows or columns in the text area.
void insertText(String, int)
Inserts the specified text at the specified position.
void replaceText(String, int, int)
Replaces text from the indicated start position (the first integer) to the indicated end position.


Previous | Next | Trail Map | Creating a User Interface | Using Components, the GUI Building Blocks