Using AWT TextArea in Java - Methods and Constructors

Introduction java.awt.TextArea Constructors and Methods

Here is an example of using TextArea in AWT in Java. These examples show you how to create AWT TextAreas using all the constructors and will also introduce some methods of the java.awt.TextArea class that are useful for further implementation.

What is a TextArea?

A Text Area is a GUI component that is used to store multiple rows of text unlike a Text Field.

java.awt.TextArea Constructors

  public TextArea()
  public TextArea(String text)
  public TextArea(int rows, int columns)
  public TextArea(String text, int rows, int columns)
  public TextArea(String text, int rows, int columns, int scrollbars)

All the constructors throw java.awt.HeadlessException.

java.awt.TextArea Methods

Insert text in the textarea at the particular position.
  public void insert(java.lang.String text, int position);
  public synchronized void insertText(java.lang.String text, int position);

Append text in the textarea.
  public void append(java.lang.String text);
  public synchronized void appendText(java.lang.String text);

Replace text between given start and end points with the given text.
  public void replaceRange(java.lang.String text, int start, int end);
  public synchronized void replaceText(java.lang.String text, int start, int end);

Set/Get no.of rows in the TextArea.
  public int getRows();
  public void setRows(int rows);

Set/Get no.of columns in the TextArea.
  public int getColumns();
  public void setColumns(int columns);

Get the Scrollbar constant value for a textarea. See down for Scrollbar constants.
  public int getScrollbarVisibility();

Set/Get Text in the TextArea. If text exists, then the text in it will be replaced with the given text.
  public void setText(String text);
  public String getText();

java.awt.TextArea Constants

Scrollbars Constants
  public static final int SCROLLBARS_BOTH; (Value 0)
  public static final int SCROLLBARS_VERTICAL_ONLY; (Value 1)
  public static final int SCROLLBARS_HORIZONTAL_ONLY; (Value 2)
  public static final int SCROLLBARS_NONE; (Value 3)

The Example
import java.awt.*;
class JavaAWTTextArea extends Frame

TextArea ta,ta1,ta2,ta3,ta4;

public JavaAWTTextArea()

setTitle("AWT TextArea Demo");
setLayout(new FlowLayout());
// Create textarea using first constructor
ta=new TextArea();

// Create textarea using second constructor
ta1=new TextArea("");

// Create textarea using third constructor
ta2=new TextArea(10,25);

// Create textarea using fourth constructor
ta3=new TextArea("",10,25);

// Create textarea using fifth constructor
ta4=new TextArea("",10,25,TextArea.SCROLLBARS_HORIZONTAL_ONLY);

// Using methods

        // Non synchronized method
ta1.append("I am appended.");
// Synchronized method
ta2.appendText("I am appended.");

// Synchronized method

// Non Synchronized method

// Set no. of rows
// Get no.of rows
System.out.println("No.of rows of ta "+ta.getRows());

// Set no.of columns

// Get no.of columns
System.out.println("No. of cols for ta "+ta.getColumns());

// Get Scrollbar Visibilty
System.out.println("Scrollbar for ta4 "+ta4.getScrollbarVisibility());
// Replace text (Synchronized)

// Replace text (Non Synchronized)

// Add textareas
public static void main(String args[])
new JavaAWTTextArea();


No.of rows of ta 10
No. of cols for ta 10
Scrollbar for ta4 2

Next: Creating AWT Checkbox in Java
Previous: Creating AWT TextField in Java

Posted by at on

Tags: Abstract Window Toolkit,