In this tutorial you will learn about the Swing JTextArea and its application with practical example.
Swing JTextArea
JTextArea is a multi line component which allows user for editing. It extends JTextComponent. The JTextArea internally handles Scrolling.
JTextArea Class Declaration
1 |
public class JTextArea extends JTextComponent |
JTextArea Constructor
Some commonly useable constructors are listed below.
JTextArea : This constructor is used to construct new Text Area.
JTextArea(Document doc,String text,int row, int columns): This constructor is used to construct a Text Area which used the given text storage model and the given number of rows and columns.
JTextArea(int rows, int columns) :This constructor is used to construct a empty Text Area with given number of rows and columns.
JTextArea(String text): This constructor is used to construct a Text Area initialized with specific text.
JTextArea(String text,int rows, int columns): This constructor is used to construct a Text Area initialized with specific text and number of rows and columns.
JTextArea Methods
Some of Commonly useable methods are listed below.
void append(String str): This method is used to append or add text at the end of document.
AccessibleContext getAccessableContext(): This method is used to gets the AccessibleContext which is associated with JTextArea.
int getLineCount():This method is used to get number of lines into the JTextArea and return will be in Integer.
int getColumns(): This method is used to get number of columns of particular Text Area and return type is Integer.
protected int getColumnWidth(): This method simply returns the column width in integer.
Dimension getPreferredSize(): This method simply returns the preferred size of JTextArea.
int getHorizontalAlignement(): This method return integer value of horizontal alignment of the text.
void setFont(Font f): This method is used to sets the current font.
void insert(String str, int pos): With the help of this method we can insert any string at specified position.
Example :-
Let’s do coding for Text Area, With the help of below program you can simply get understand about JTextArea.
W3AddASwing.java
Create a class and put down below code into the class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package w3addaswing; import javax.swing.JFrame; import javax.swing.JTextArea; public class W3ADDASwing { public static void main(String[] args) { // TODO code application logic here JFrame frame = new JFrame("TextArea Example W3ADDA"); JTextArea jArea=new JTextArea("Welcome to W3adda.com. One Stop Tutorials point"); jArea.setBounds(10,30, 200,200); frame.add(jArea); frame.setSize(400, 400); frame.setLayout(null); frame.setVisible(true); } } |
Output :-
Output of this code will look like below image.
This was simple article to get understand about JTextArea. I Hope from this article you got clear idea about JTextArea.