Swing JTextArea

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.

Table Of Contents

JTextArea Class Declaration

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.

Output :-

Output of this code will look like below image.

image001

This was simple article to get understand about JTextArea. I Hope from this article you got clear idea about JTextArea.

In this tutorial we have learn about the Swing JTextArea and its application with practical example. I hope you will like this tutorial.