Home » Java programming language

Java AWT TextArea

Java | AWT TextArea: In this tutorial, we will look at one of the Java AWT components, the AWT TextArea with example.
Submitted by Saranjay Kumar, on April 29, 2020

Till now all the components we have looked at provide the functionality of displaying only a short text. The TextField provides only a single line message to be displayed. But in many practical cases, we require not a single line but an entire paragraph of text to be displayed. For such cases, we have the TextArea class. It provides a multi-line editor area. Users can interact with the text. Whenever the user tries to enter the text that is more than what can be displayed in a given area, a scrollbar appears automatically and allows the user to scroll through the entire text.

Consider the following code -

import java.awt.*;

public class CreateTextArea{

     CreateTextArea()
     {
         Frame f = new Frame();
          
         TextArea t1 = new TextArea("This is text area 1.\n It has scrollbars\n on both sides.");
         TextArea t2 = new TextArea("This is text area 2.\n It has horizontal scrollbar only.", 2, 10, TextArea.SCROLLBARS_HORIZONTAL_ONLY);
         TextArea t3 = new TextArea();
         
         t3.setText("Adding new Text...");
         
         t1.setBounds(50,50, 100, 60);
         t2.setBounds(50,150, 100, 60);
         t3.setBounds(50, 250, 100, 50);
         
         f.setLayout(null);
         f.setVisible(true);
         f.setSize(300,300);
         
         t3.append("\nRows in t1 " + t1.getRows() + "\nColumns in t1 " + t1.getColumns());
                           
         f.add(t1);
         f.add(t2);
         f.add(t3);
     
     }
                  
        public static void main(String []args){
        CreateTextArea ob = new CreateTextArea();
        
     }
}

Output

Java AWT TextArea

We created 3 objects of the TextArea class. T1 is initialized with a String value. This is the text that is displayed on object creation. T2 has been created with a String value, the row size, the column size, and the scrollbars that will be visible in the TextArea. We have set the number of rows to be displayed as 2 and the number of columns as 10. By default, a TextArea has both the vertical and the horizontal scrollbars. However, in t2, we create and display only the horizontal scrollbar. The various values that can be set for this field are as follows -

    static int SCROLLBARS_BOTH - Both vertical and horizontal scrollbars
    static int SCROLLBARS_NONE - No scrollbars
    static int SCROLLBARS_HORIZONTAL_ONLY -Horizontal scrollbar only
    static int SCROLLBARS_VERTICAL_ONLY -Vertical scrollbar only

The setText() method is used to set the text that is displayed in the text area. Users can add, delete, append, and interact with the text being displayed. We can retrieve the text of the TextArea by using the getText() method. These methods are the same as in the TextField class.

The append() method is used to add/append text on the screen. Text is added at the end of the lines already present in the text area.

    void append(String str)

The getRows() method returns an integer value, specifying the number of rows present in the text area.

    int getRows()

Similarly, the getColumns() method is used to retrieve the number of columns present in a text area.

    int getColumns()


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.