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

Java TextArea Multiple Lines Text

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.

4 thoughts on “Java TextArea Multiple Lines Text”

  1. SangeetaVaidya

    Respected Sir,

    Am a working parent of 3 kids so in class tuition is not possible.

    Do you teach online via Skype or GotoMeeting ?

    OR

    Can we BUY CD/DVD of ALL your classes ?

    If no then is there any institute in Hyderabad that does ONLINE JAVA coaching?

    Please help.

    Thanks

    Sangeeta Vaidya

Leave a Comment

Your email address will not be published.