13.9 TextAreas

A TextArea is similar to a TextField, except that it can have multiple lines and action events are not generated. Here are some typical examples:

      TextArea inputArea = new TextArea(4, 15);
      add(inputArea);
      TextArea resultsArea = 
        new TextArea("No Results Yet", 2, 10);
      resultsArea.setEditable(false);
      add(resultsArea);

Constructors

public TextArea()

This creates an empty text area with a platform-dependent number of rows and columns and both vertical and horizontal scrollbars. The default size may be very large, so use this constructor with caution if you're not using a layout manager that will size it for you.

public TextArea(int rows, int cols)

This creates an empty text area with the specified number of rows and columns. The column value is set based on the average width of characters in whatever font the TextArea is using. Vertical and horizontal scrollbars are included.

public TextArea(String initialString)

This creates an initialized text area with a platform-dependent size and both types of scrollbars. The initial string can contain explicit line breaks (\n) to force it to span multiple lines.

public TextArea(String initialString, int rows, int cols)

This creates an initialized text area with the specified size and both types of scrollbars.

public TextArea(String initialString, int rows, int cols, int scrollbarType) [Java 1.1]

This creates an initialized text area with the specified size. If the scrollbar type is TextArea.SCROLLBARS_BOTH, the result is the same as with the previous constructor. If TextArea.SCROLLBARS_VERTICAL_ONLY is specified, the text area will have vertical but not horizontal scrollbars, and words will wrap to the next line if the user enters text that would otherwise extend past the right side of the text area. If TextArea.SCROLLBARS_HORIZONTAL_ONLY is supplied, the text area will have horizontal but not vertical scrollbars. Finally, if you specify TextArea.SCROLLBARS_NEITHER, no scrollbars will be included. Again, text will wrap as it is entered. Java 1.0 text areas always have both scrollbars and never automatically wraps words.

Example

Listing 13.29 creates two text areas; an empty one and another filled with three initial lines. Figures 13-25, 13-26, and 13-27 show the results on Windows 95, MacOS, and Solaris.

Listing 13.29 TextAreas.java

import java.applet.Applet;
import java.awt.*;

public class TextAreas extends Applet {
  public void init() {
    setBackground(Color.lightGray);
    add(new TextArea(3, 10));
    add(new TextArea("Some\nInitial\nText", 3, 10));
  }
}

Figure 13-25 Text areas in Windows 95.



Figure 13-26 Text areas in MacOS.



Figure 13-27 Text areas in Solaris.



Other TextArea Methods

TextArea inherits getText, setText, setEditable, select, and all of the other methods of TextComponent described in Section 13.8 (TextFields). In addition, they support the following methods.

public void appendText(String additionalText)
public void append(String additionalText) [Java 1.1]

This adds the specified text to the end of the text area.

public int getColumns()

This returns the number of columns in the text area.

public int getRows()

This returns the number of rows used to create the text area.

public void insertText(String additionalText, int index)
public void insert(String additionalText, int index) [Java 1.1]

These methods insert the specified text at the given index. Any text at that location or later is moved down.

public void replaceText(String replacement, int startIndex, int endIndex)
public void replaceRange(String replacement, int startIndex, int endIndex) [Java 1.1]

This replaces the text starting at startIndex, up through but not including endIndex, with the specified string. The replacement string need not be the same length as the text being replaced.

As a subclass of Component (Section 11.2), TextArea has access to all Component methods. Color support for text areas is quite consistent across platforms. Foreground and background colors are supported in Netscape (2, 3, and 4, where released), Internet Explorer (version 3), and Sun's JDK (1.0 and 1.1) on Windows 95/NT, MacOS, and Unix/X11.

Handling Events

Text areas do not generate action events. Keyboard, text (Java 1.1 only), and focus events are handled in exactly the same manner as with textfields.


Continue to Section 13.10 (Labels). Return to Chapter 13 table of contents.