Way2Java

Java TextArea Multiple Lines Text

Java TextArea overcomes the limitation of TextField and is used to display or take input in multiple lines of text. Following list illustrates the limitations.

Differences between TextField and TextArea
  1. TextField displays only one line of text of any length. TextArea displays multiple lines of text of any length.
  2. TextField generates ActionEvent handled by ActionListener and TextArea generates TextEvent handled by TextListener.

Following is the hierarchy of TextArea component

As can be observed from the above figure, both TextField and TextArea are descendents of TextComponent. Both can utilize the methods of TextComponent like setEditable() and getText() etc. This is the advantage of learning super class methods.

Any action, like typing, in TextArea generates TextEvent. TextEvent handler is TextListener. TextListener includes only one abstract method textValueChanged(TextEvent) and should be overridden with the event handling code.

The following program creates two Java TextArea Multiple Lines Text. What is typed in one text area is displayed in the other.
import java.awt.*;
import java.awt.event.TextListener;
import java.awt.event.TextEvent;

public class TADemo extends Frame implements TextListener
{
  TextArea typeText, displayText;
  public TADemo()
  { 
    super("Practicing Text Area");
                     // default BorderLayot is used
    typeText = new TextArea("Type Here", 10, 20);                     
    displayText = new TextArea();                     
    displayText.setRows(10);
    displayText.setColumns(20);

    typeText.addTextListener(this);

    add(typeText, BorderLayout.NORTH);
    add(displayText, BorderLayout.SOUTH);

    setSize(300, 350);
    setVisible(true);
  }
  public void textValueChanged(TextEvent e)
  {
    String str =  typeText.getText();
    displayText.setText(str);
  }
  public static void main(String args[])
  {
    new TADemo();
  }
}

Two TextArea objects typeText and displayText are created one for typing into and the other to display what is typed in the earlier.

Java TextArea Multiple Lines Text

super("Practicing Text Area");

The above statement does the same purpose of setTitle(). It is another style of giving the title to the frame. We know earlier, super() statement must be the first one in the constructor if exists.

typeText = new TextArea("Type Here", 10, 20);

The TextArea object typeText is created to display 10 rows and 20 columns with a string "Type Here" that is displayed by default. If the string is omitted in the constructor, an empty text area is obtained.

displayText = new TextArea();
displayText.setRows(10);
displayText.setColumns(20);

The TextArea object displayText is created in a different way. An empty constructor is used and the properties like rows and columns are set separately with setRows() and setColumns() methods.

In contrast to a default constructor, the parameterized constructor gives the object, by default, properties at the time of creation itself. This style of creating object is easier than using a default constructor. The above set() methods can be used advantageously to change, dynamically, the size of the text area etc.

typeText.addTextListener(this);

As usual, the component typeText is registered with TextListener with addTextListener() method of TextArea. The "this" parameter represents an object of TextListener.

add(typeText, BorderLayout.NORTH);
add(displayText, BorderLayout.SOUTH);

typeText and displayText objects are added to frame, set with BorderLayout, in a different way by using the final static variables NORTH and SOUTH instead of general "North" and "South".

String str = typeText.getText();
displayText.setText(str);

With getText() method, the existing data in the text area typeText is obtained and is set (placed) in the text area displayText with method setText() mehod.

TextArea includes three methods to place the text. They are

  1. void append(String str) : Appends the string str to the existing data in the text area.
  2. void replaceRange(String str, int start, int end) : Replaces the existing text between the start and end positions with string str.
  3. void setText(String str) : Displays the text str in the text area. Inherited from its super class TextComponent. String str overrides all the earlier data. That is, the earlier data is lost. To retain earlier data, use append(String).
    method.

One more method appendText(String) also exists which differs from append() method in that appendText() is synchronized; but for both are same. The design pattern and internals of handling events is discussed figuratively in Event Handling.