5.2.5 The TextField and TextArea Class


The following example is an incomplete fill-out form for personal data.

The Applet

The Java Code
import java.applet.Applet;
import java.awt.*;

public class PersonalData extends Applet {

  public void init() {
    //  add name label plus text field
    add(new Label("Name", Label.RIGHT));
    TextField name = new TextField("Surname, Initials", 25);
    add(name);
    //  add address label plus text area
    add(new Label("Address", Label.RIGHT));
    TextArea address = new TextArea(10,25);
    add(address);
  }
}

The GUI components used are:

Label
This class had been discussed in the first subsection of this chapter. It represents text that cannot be changed by the user.

TextField
A text field is a component in which you can enter a single editable line of text. One way to create a text field is via TextField(String text, int numberOfCharacters). In this way you get a new text field with the specified text and wide enough to hold the specified number of characters.

TextArea
A text area is multi-line area for displaying text. It can be set to allow editing or to be read-only. In the above example we have created a text area of 10 lines long and 30 characters wide.

Both TextField and TextArea are subclasses of TextComponent and they inherit most of their methods from this parent class such as methods for selecting and changing text We summarize the constructors and main methods for text areas, text fields, and text components.

TextField

Constructors Meaning
TextField() creates an empty text field
TextField(int numberOfCharacters) creates an empty text area with the given number of character positions (characters)
TextField(String text) creates a text field displaying the specified text
TextField(String text, int numberOfCharacters) creates a text field with specified dimension and text
Methods Meaning
getColumns() returns the width of the text field
getEchoChar() returns the echo character
setEchoChar() sets the echo character
Other methods are inherited from TextComponent.

TextArea

Constructors Meaning
TextArea() creates an empty text area
TextArea(int numberOfRows, int numberOfColumns) creates an empty text area with the given number of rows and columns (characters)
TextArea(String text) creates a text area displaying the specified text
TextArea(String text, int numberOfRows, int numberOfColumns) creates a text area with specified dimension and text
Methods Meaning
appendText(String text) appends the specified text
getColumns() returns the number of columns
getRows() returns the number of rows
insertText(String text, int position) inserts the specified text at the given character position
replaceText(String text, int start, int end) replaces the text from start to end position with the specified new text
Other methods are inherited from TextComponent.

TextComponent

Methods Meaning
getSelectedText() returns the selected text
getSelectionEnd() returns the selected text's end position
getSelectionStart() returns the selected text's start position
getText() returns the text of the text component
select(start, int end) selects lines from the specified start position (inclusive) to the end position (exclusive)
selectAll() selects all text in the field
setEditable(boolean state) enable (true) or disable (false) text to be edited
setText(String text) sets the specified text