AWT TextArea Class

The TextArea control in AWT provide us multiline editor area. The user can type here as much as he wants. When the text in the text area become larger than the viewable area the scroll bar is automatically appears which help us to scroll the text up & down and right & left.
Following is the declaration for java.awt.TextArea class:
public class TextArea
   extends TextComponent
Following are the fields for java.awt.TextArea class:
static int SCROLLBARS_BOTH  -- Create and display both vertical and horizontal scrollbars. 	
static int SCROLLBARS_BOTH  -- Create and display both vertical and horizontal scrollbars. 	
static int SCROLLBARS_HORIZONTAL_ONLY  -- Create and display horizontal scrollbar only.
static int SCROLLBARS_HORIZONTAL_ONLY  -- Create and display horizontal scrollbar only.
static int SCROLLBARS_NONE  -- Do not create or display any scrollbars for the text area. 
static int SCROLLBARS_NONE  -- Do not create or display any scrollbars for the text area. 
static int SCROLLBARS_VERTICAL_ONLY -- Create and display vertical scrollbar only.
static int SCROLLBARS_VERTICAL_ONLY -- Create and display vertical scrollbar only.
TextArea() 
Constructs a new text area with the empty string as text.
TextArea(int rows, int columns) 
Constructs a new text area with the specified number of rows and columns and the empty string as text.
TextArea(String text) 
Constructs a new text area with the specified text.
TextArea(String text, int rows, int columns) 
Constructs a new text area with the specified text, and with the specified number of rows and columns.
TextArea(String text, int rows, int columns, int scrollbars) 
Constructs a new text area with the specified text, and with the rows, columns, and scroll bar visibility as specified.
void addNotify() 
Creates the TextArea's peer.
void append(String str) 
Appends the given text to the text area's current text.
void appendText(String str) 
Deprecated. As of JDK version 1.1, replaced by append(String).
AccessibleContext getAccessibleContext() 
Returns the AccessibleContext associated with this TextArea.
int getColumns() 
Returns the number of columns in this text area.
Dimension getMinimumSize() 
Determines the minimum size of this text area.
Dimension getMinimumSize(int rows, int columns) 
Determines the minimum size of a text area with the specified number of rows and columns.
Dimension getPreferredSize() 
Determines the preferred size of this text area.
Dimension getPreferredSize(int rows, int columns) 
Determines the preferred size of a text area with the specified number of rows and columns.
int getRows() 
Returns the number of rows in the text area.
int getScrollbarVisibility() 
Returns an enumerated value that indicates which scroll bars the text area uses.
void insert(String str, int pos) 
Inserts the specified text at the specified position in this text area.
void insertText(String str, int pos) 
Deprecated. As of JDK version 1.1, replaced by insert(String, int).
Dimension minimumSize() 
Deprecated. As of JDK version 1.1, replaced by getMinimumSize().
Dimension minimumSize(int rows, int columns) 
Deprecated. As of JDK version 1.1, replaced by getMinimumSize(int, int).
protected String paramString() 
Returns a string representing the state of this TextArea.
Dimension preferredSize() 
Deprecated. As of JDK version 1.1, replaced by getPreferredSize().
Dimension preferredSize(int rows, int columns) 
Deprecated. As of JDK version 1.1, replaced by getPreferredSize(int, int).
void replaceRange(String str, int start, int end) 
Replaces text between the indicated start and end positions with the specified replacement text.
void replaceText(String str, int start, int end) 
Deprecated. As of JDK version 1.1, replaced by replaceRange(String, int, int).
void setColumns(int columns) 
Sets the number of columns for this text area.
void setRows(int rows) 
Sets the number of rows for this text area.
This class inherits methods from the following classes:
java.awt.TextComponent
java.awt.TextComponent
java.awt.Component
java.awt.Component
java.lang.Object
java.lang.Object
Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtControlDemo {

   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtControlDemo  awtControlDemo = new AwtControlDemo();
      awtControlDemo.showTextAreaDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showTextAreaDemo(){
      headerLabel.setText("Control in action: TextArea"); 

      Label  commentlabel= new Label("Comments: ", Label.RIGHT);

      final TextArea commentTextArea = new TextArea("This is a AWT tutorial "
      +"to make GUI application in Java.",5,30);

      Button showButton = new Button("Show");

      showButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {     
            statusLabel.setText( commentTextArea.getText());        
         }
      }); 

      controlPanel.add(commentlabel);
      controlPanel.add(commentTextArea);        
      controlPanel.add(showButton);
      mainFrame.setVisible(true);  
   }
}
Compile the program using command prompt. Go to D:/ > AWT  and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtControlDemo
Verify the following output

    13 Lectures 
     2 hours 

 EduOLC
 Print
 Add Notes
 Bookmark this page