JTextArea is a component in Java Swing used for displaying and editing multi-line plain text.javax.swing package and extends JTextComponent, which provides standard text operations like cut, copy, paste, and undo.
| Constructor | Description |
|---|---|
JTextArea() |
Creates a new JTextArea with no text and default number of rows and columns. |
JTextArea(String text) |
Creates a new JTextArea with the specified initial text. |
JTextArea(int rows, int columns) |
Creates a new JTextArea with the specified number of rows and columns and no initial text. |
JTextArea(String text, int rows, int columns) |
Creates a new JTextArea with the specified initial text and the specified number of rows and columns. |
JTextArea(Document doc) |
Creates a new JTextArea that displays the content from the specified Document. |
JTextArea(Document doc, String text, int rows, int columns) |
Creates a new JTextArea with the specified initial text, the specified number of rows and columns, and the specified Document. |
package jtextarea;
import javax.swing.*;
public class JTextAreaExample {
public static void main(String[] args) {
// Create a new JFrame
JFrame frame = new JFrame("JTextArea Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setLayout(null);
// JTextArea with no text and default size
JTextArea textArea1 = new JTextArea();
textArea1.setBounds(20, 20, 200, 100);
frame.add(textArea1);
// JTextArea with initial text
JTextArea textArea2 = new JTextArea("Initial Text");
textArea2.setBounds(250, 20, 200, 100);
frame.add(textArea2);
// JTextArea with specified rows and columns
JTextArea textArea3 = new JTextArea(5, 20);
textArea3.setBounds(20, 150, 200, 100);
frame.add(textArea3);
// JTextArea with initial text, rows, and columns
JTextArea textArea4 = new JTextArea("Text with rows and columns", 5, 20);
textArea4.setBounds(250, 150, 200, 100);
frame.add(textArea4);
// Set some properties using methods
textArea1.setLineWrap(true);
textArea1.setWrapStyleWord(true);
textArea1.setTabSize(4);
textArea1.setText("Updated text");
textArea1.append("nAppended text");
textArea2.setEditable(false);
textArea3.insert("Inserted text", 0);
textArea4.replaceRange("Replaced text", 5, 10);
// Display the frame
frame.setVisible(true);
}
}

| Method | Description |
|---|---|
append(String str) |
Appends the given text to the end of the document. |
insert(String str, int pos) |
Inserts the specified text at the specified position. |
replaceRange(String str, int start, int end) |
Replaces the text from the start position to the end position with the specified text. |
setText(String t) |
Sets the text of the JTextArea to the specified text. |
getText() |
Returns the text contained in this JTextArea. |
setLineWrap(boolean wrap) |
Sets the line-wrapping policy of the text area. |
getLineWrap() |
Returns whether the text area is set to wrap lines. |
setWrapStyleWord(boolean word) |
Sets the style of wrapping used if the text area is wrapping lines. |
getWrapStyleWord() |
Returns whether the text area wraps lines at word boundaries. |
setRows(int rows) |
Sets the number of rows for this text area. |
getRows() |
Returns the number of rows preferred for this text area. |
setColumns(int columns) |
Sets the number of columns for this text area. |
getColumns() |
Returns the number of columns preferred for this text area. |
setTabSize(int size) |
Sets the number of characters to expand tabs to. |
getTabSize() |
Returns the number of characters to expand tabs to. |
setEditable(boolean b) |
Sets the JTextArea to be editable or not. |
isEditable() |
Returns whether the JTextArea is editable. |
getLineCount() |
Returns the number of lines in the text area. |
getLineOfOffset(int offset) |
Returns the line number corresponding to the given character offset. |
getLineStartOffset(int line) |
Returns the starting offset of the specified line. |
getLineEndOffset(int line) |
Returns the ending offset of the specified line. |
getRows() |
Returns the number of rows in the JTextArea. |
getColumns() |
Returns the number of columns in the JTextArea. |
You must be logged in to submit a review.