About the JTextArea Example

JTextArea with Word & Line Wrap

1. JTextArea Component

The JTextArea component in swing is useful to deal with multiple line of texts. Remember JTextField can take only a single line of text. To support multi-line of texts, one can use the Java Swings’ JTextArea component. This Control often used with the JScrollPane to get the scrolling support. We can also set word-wrap and line-wrap, which will be good when the horizontal scrolling is disabled.

2. About the JTextArea Example

The example which we will create is below:

About the JTextArea Example
The JTextArea Example

The finished example is showing a JTextArea component. You can also see how it is showing the multi-line text in it. Here in the above picture, complete text content is not visible in the component. But, the component has a scroll bar which we can scroll to view the remaining content. Our finished example display only the vertical scroll bar and when we progress through the example, we will see how to provide the horizontal scroll bar as well.

3. Creating JTextArea

The below code present in the constructor of JFrame window. It shows how we can create JTextArea component.

At line 7, we create the JTextArea, and we pass two integers to the constructor. The first param tells how many rows of text will be visible in the component. Java Swing will resize the JTextArea so that it can show a specified number of lines without scrolling. In our case, it is 11 rows. The second param specifies number column in terms of characters. In our example, the Swing will adjust the width of the JTextArea so that it can show 30 characters without scrolling the content horizontally. So, swing will calculate the width and height based on these two params.

At this stage, if we execute the application, it will display JTextArea as in the below picture:

JTextArea Without Scrolling
JTextArea Without Scrolling

At this stage, if we type more than 30 characters in a line, the JTextArea component will expand width-wise, and it even goes beyond the frame window. In the same way, if we type the text exceeding 11 lines, the JTextArea will grow height-wise. Now, we will avoid this by using the JScrollPane.

4. Scrolling Support via JScrollPane

Now we will change our existing code. The changed code is shown below:

At line 13-16, we create the JScollPane, and we mention having both the scrollbars always. The first param is the component which wants to scroll. In our case, we said that we need scrolling support for JTextArea as it is the first param here. Note, first we comment our code at line number 10. Means, we are not giving the JTextArea to the JFrame. Instead, we add the JScrollPane to the JFrame Window by calling the add method at line 17.

Now, we run the example and JTextArea looks like below:

JScrollPane & JTextArea
JScrollPane & JTextArea

Now the JFrame is displaying the JScrollPane with vertical and horizontal scrollbars. Note, we added the JScrollPane to the JFrame window. We gave the JScrollPane to the JTextArea during its construction time. So, the scroll pane takes care of rendering the JTextArea. Not only that! It provided the scrolling support for the JTextArea. We can scroll the JTextArea horizontally as well as vertically.

In the above screenshot, to read line 4 and 8, we need to scroll the TextArea horizontally. Then scroll back to the original position to read the next line. This will be annoying, and it will be nice if we have only vertical scrolling support.

5. JTextArea Word Wrap & Line Wrap

The moment we remove the horizontal scrollbar and keep only vertical scroll, we end up using the Line-Wrap and Word-Wrap for the JTextArea component. Completed version of the code is below:

You can notice we removed the horizontal scroll bar using the constant HORIZONTAL_SCROLLBAR_NEVER while we construct the JScrollPane (Refer Line No 11). Now, when number characters exceed the width of the JTextArea, we want to see text in the next line instead of extending it in the same line. The user can, of course, hit the enter button to move the text to next line. But what about when we read a text document from the file to JTextArea?

The call setLineWrap at line 17, sets the Line-Wrap property. And in the next line, we set word-wrap as well by calling the method setWrapStyleWord. Now have a look at the below picture:

JTextArea - Wordwrap and Linewrap
JTextArea – Word-wrap and Line-Wrap

In the above picture, we can assume Red and Blue boxes are words. Here, you can see the lines are wrapping via the method call setLineWrap and we have four lines of texts. The blue rectangle shows how a word is broken in two lines. To avoid this, we set the word-wrap by calling the method setWrapStyleWord.

6. Watch This Example as YouTube Video

7. Code Reference



Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.