JTextArea is a part of java Swing package . It represents a multi line area that displays text. It is used to edit the text .
JTextArea inherits JComponent class. The text in JTextArea can be set to different available fonts and can be appended to new text . A text area can be customized to the need of user .
Constructors of JTextArea are:
- JTextArea() : constructs a new blank text area .
- JTextArea(String s) : constructs a new text area with a given initial text.
- JTextArea(int row, int column) : constructs a new text area with a given number of rows and columns.
- JTextArea(String s, int row, int column) : constructs a new text area with a given number of rows and columns and a given initial text.
Commonly used methods :
- append(String s) : appends the given string to the text of the text area.
- getLineCount() : get number of lines in the text of text area.
- setFont(Font f) : sets the font of text area to the given font.
- setColumns(int c) : sets the number of columns of the text area to given integer.
- setRows(int r) : sets the number of rows of the text area to given integer.
- getColumns() : get the number of columns of text area.
- getRows() : get the number of rows of text area.
1. Program to create a simple JTextArea
Output:
👁 Image
2. Program to create a JTextArea and set a initial text and add buttons to change the font of text area.