Java Swing | JTextArea
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
2. Program to create a JTextArea and set a initial text and add buttons to change the font of text area.
Note : The following program might mot run in an online compiler please use an offline IDE
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.