«Back to Home

Core Java

Topics

JTextArea Class In Java

JTextArea Class
 
In Java, The JTextArea class creates a text area and it is a multiline area, which displays the plain text only.
 
Constructors of JTextArea class
 
JTextArea()
 
This constructor creates a text area, which displays no text initially.
 
JTextArea(String s)
 
This constructor creates a text area, which displays particular text initially.
 
JTextArea(int row, int column)
 
This constructor creates a text area with the particular number of rows and columns, which displays no text initially.
 
JTextArea(String s, int row, int column)
 
This constructor creates a text area with the particular number of rows and columns, which displays the specified text.
 
Methods of JTextArea class
 
public void setRows(int rows)
 
This method is used to set the particular number of rows.
 
public void setColumns(int cols)
 
This method is used to set the particular number of columns.
 
public void setFont(Font f)
 
This method is used to set the particular font.
 
public void insert(String s, int position)
 
This method is used to insert the specified text on the particular position.
 
public void append(String s)
 
This method is used to append the given text to the end of the document.
 
Let’s see an example of JTextField class, given below.
 
Code
  1. import java.awt.Color;  
  2. import javax.swing.*;  
  3. public class JTextAreaExample {  
  4.     JTextArea area;  
  5.     JFrame jf;  
  6.     JTextAreaExample() {  
  7.         jf = new JFrame();  
  8.         area = new JTextArea(400400);  
  9.         area.setBounds(1030300300);  
  10.         area.setBackground(Color.black);  
  11.         area.setForeground(Color.white);  
  12.         jf.add(area);  
  13.         jf.setSize(400400);  
  14.         jf.setLayout(null);  
  15.         jf.setVisible(true);  
  16.     }  
  17.     public static void main(String[] args) {  
  18.         new JTextAreaExample();  
  19.     }  
  20. }  
36

Output

37
 
Summary

Thus, we learnt that JTextArea class creates a text area and it is a multiline area, which displays the plain text only and also learnt how to use it in Java.