Java JTextArea

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java JTextArea

The object of a JTextArea class is a multi line region that displays text. It allows the editing of multiple line text. It inherits JTextComponent class

JTextArea class declaration

Let's see the declaration for javax.swing.JTextArea class.

  1. public class JTextArea extends JTextComponent  

Commonly used Constructors:

Constructor

Description

JTextArea()

Creates a text area that displays no text initially.

JTextArea(String s)

Creates a text area that displays specified text initially.

JTextArea(int row, int column)

Creates a text area with the specified number of rows and columns that displays no text initially.

JTextArea(String s, int row, int column)

Creates a text area with the specified number of rows and columns that displays specified text.

 

Commonly used Methods:

Methods

Description

void setRows(int rows)

It is used to set specified number of rows.

void setColumns(int cols)

It is used to set specified number of columns.

void setFont(Font f)

It is used to set the specified font.

void insert(String s, int position)

It is used to insert the specified text on the specified position.

void append(String s)

It is used to append the given text to the end of the document.

Java JTextArea Example

  1. import javax.swing.*;  
  2. public class TextAreaExample  
  3. {  
  4.      TextAreaExample(){  
  5.         JFrame f= new JFrame();  
  6.         JTextArea area=new JTextArea("Welcome to javatpoint");  
  7.         area.setBounds(10,30200,200);  
  8.         f.add(area);  
  9.         f.setSize(300,300);  
  10.         f.setLayout(null);  
  11.         f.setVisible(true);  
  12.      }  
  13. public static void main(String args[])  
  14.     {  
  15.    new TextAreaExample();  
  16.     }}  

Java JTextArea Example with ActionListener

  1. import javax.swing.*;  
  2. import java.awt.event.*;  
  3. public class TextAreaExample implements ActionListener{  
  4. JLabel l1,l2;  
  5. JTextArea area;  
  6. JButton b;  
  7. TextAreaExample() {  
  8.     JFrame f= new JFrame();  
  9.     l1=new JLabel();  
  10.     l1.setBounds(50,25,100,30);  
  11.     l2=new JLabel();  
  12.     l2.setBounds(160,25,100,30);  
  13.     area=new JTextArea();  
  14.     area.setBounds(20,75,250,200);  
  15.     b=new JButton("Count Words");  
  16.     b.setBounds(100,300,120,30);  
  17.     b.addActionListener(this);  
  18.     f.add(l1);f.add(l2);f.add(area);f.add(b);  
  19.     f.setSize(450,450);  
  20.     f.setLayout(null);  
  21.     f.setVisible(true);  
  22. }  
  23. public void actionPerformed(ActionEvent e){  
  24.     String text=area.getText();  
  25.     String words[]=text.split("\s");  
  26.     l1.setText("Words: "+words.length);  
  27.     l2.setText("Characters: "+text.length());  
  28. }  
  29. public static void main(String[] args) {  
  30.     new TextAreaExample();  
  31. }  
  32. }