Using JTextArea in Swing

A JTextArea is a component that stores data of multiple rows. This example will cover most of the JTextArea class. Last but not least, it is part of javax.swing package.

Class Structure

public class JTextArea extends javax.swing.JTextComponent

Constructors

  public JTextArea();
  public JTextArea(String text);
  public JTextArea(int rows, int columns);
  public JTextArea(String text, int rows, int columns);
  public JTextArea(Document documentobj);
  public JTextArea(Document documentobj, String text, int rows, int columns);

Methods

  public void setTabSize(int size);
  public int getTabSize();

  public void setLineWrap(boolean flag);
  public boolean getLineWrap();

  public void setWrapStyleWord(boolean flag);
  public boolean getWrapStyleWord();

  public void insert(String text, int pos);

  public void append(String text);

  public void replaceRange(String text, int start, int end);

  public int getRows();
  public void setRows(int);

  public int getColumns();
  public void setColumns(int cols);
  public void setFont(Font font);

Example


Screenshot of JTextArea Demo

import javax.swing.*;
import java.awt.*;

class JTextAreaJava extends JFrame
{
JTextArea jt1,jt2,jt3,jt4;
JScrollPane js1,js2,js3,js4;
public JTextAreaJava()
{
setTitle("Java JTextArea");
setLayout(new GridLayout());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

jt1=new JTextArea();

/* Methods */
jt1.setText("First TextArea");
jt1.setFont(new Font("Times New Roman",Font.PLAIN,22));
jt1.append("Appended Text.");
jt1.setTabSize(20);
jt1.setRows(100);
jt1.setColumns(100);
jt1.setLineWrap(true);
jt1.setWrapStyleWord(false);
jt1.insert(" Inserted in middle. ",10);

js1=new JScrollPane(jt1);
add(js1);


jt2=new JTextArea("Second TextArea");
jt2.setFont(new Font("Times New Roman",Font.PLAIN,22));
jt2.append(" Appended Text Second TextArea ");
jt2.setLineWrap(true);
jt2.setWrapStyleWord(true);

js2=new JScrollPane(jt2);
add(js2);

jt3=new JTextArea(100,50);
jt3.setText("Third TextArea");
jt3.setFont(new Font("Times New Roman",Font.PLAIN,22));

js3=new JScrollPane(jt3);
add(js3);

jt4=new JTextArea("Fourth TextArea",100,50);
jt4.setFont(new Font("Times New Roman",Font.PLAIN,22));
jt4.replaceRange("Forth", 0,6);

js4=new JScrollPane(jt4);
add(js4);
setExtendedState(MAXIMIZED_BOTH);
}

public static void main(String args[])
{
new JTextAreaJava();
}
}

jt1.setText("First TextArea") : Set "First TextArea" in jt1.

jt1.setFont(new Font("Times New Roman"),Font.PLAIN,22): Set font for the jtextarea jt1. The font, Times New Roman, style: PLAIN, size: 22.

jt1.append("Appended Text."): This text will be appended at last to jt1.

jt1.setLineWrap(true): Enable linewrap, i.e. if the text reaches end of line, then remaining text gets in new line.

jt1.setWrapStyleWord(false): Set word style to false, so that if a word reaches the end and is not completed, the whole word doesn't come in the new line. You can understand it if you are familiar with WordWrap in Notepad.

jt1.insert(" Insert in middle. ",10): Insert text at the specified position, here 10

jt4.replaceRange("Forth",0,6): Replace text with Forth with the text between 0 and 6.