Java Tutorial

AWT Text Area

Text Area is the GUI interactive component in which we can enter multiple lines of information such as addresses, remarks, achievements, etc.

Creating a TextArea is nothing but creating an object of TextArea class. Whenever we create a TextArea component by default Scrollbars will be in invisible mode. Whenever number of rows are exceeding automatically we get vertical scrollbar whereas when number of columns are exceeding we get horizontal scrollbar.

Text Area API:

Data members:

public static final int SCROLLBARS_NONE; 
public static final int SCROLLBARS_BOTH;
public static final int SCROLLBARS_VERTICAL_ONLY; 
public static final int SCROLLBARS_HORIZONTAL_ONLY;

Constructors:

TextArea ();
TextArea (int rows, int cols);
TextArea (int rows, int cols, int scroll visibility modifier);

Instance methods:

public void setRows (int rows); 
public void setColums (int cols); 
public int getRows ();
public int getCols ();
public void setScrollbarVisibility (int); 
public int getScrollbarVisibility (); 
public void setText (String);
public String getText ();
public void append (String);
public void replace (int start pos, int end pos, String); 
public void addTextListener (TextListener); 7registered 
public void removeTextListener (TextListener); 7unregistered

Write a java program which illustrates the concept of TextArea?

Answer:

import java.awt.*; 
import java.awt.event.*;
import java.applet.Applet.*;
/*<applet code="TextareaApp" height=200 width=200>
</applet>*/
public class TextareaApp extends Applet
{
    Label l1, l2; TextField tf; TextArea ta;
    public void init ()
    {
        setBackground (Color.yellow); 
        l1=new Label ("Enter a text"); 
        l2=new Label ("Copied text"); 
        tf=new TextField (20);
        ta=new TextArea ();
        add (l1);
        add (l2);
        add (tf);
        add (ta);
    }
    public void start ()
    {
        tf.addTextListener (new tl ());
    }
    class tl implements TextListener
    {
        public void textValueChanged (TextEvent te)
        {
            ta.setText ("");
            String s=tf.getText (); 
            ta.append (s+"\n");
        }
    }
};