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
- import java.awt.Color;
- import javax.swing.*;
- public class JTextAreaExample {
- JTextArea area;
- JFrame jf;
- JTextAreaExample() {
- jf = new JFrame();
- area = new JTextArea(400, 400);
- area.setBounds(10, 30, 300, 300);
- area.setBackground(Color.black);
- area.setForeground(Color.white);
- jf.add(area);
- jf.setSize(400, 400);
- jf.setLayout(null);
- jf.setVisible(true);
- }
- public static void main(String[] args) {
- new JTextAreaExample();
- }
- }
Output
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.