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:

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.
1 2 3 4 5 6 7 8 9 10 |
//Sample 01: Set Size and Position setBounds(100, 100, 500, 300); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create Text Area JTextArea jta = new JTextArea(11, 30); //Sample 03: Add to Content Pane (Step Run 1) ControlHost.add(jta); |
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:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//Sample 01: Set Size and Position setBounds(100, 100, 500, 300); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create Text Area JTextArea jta = new JTextArea(11, 30); //Sample 03: Add to Content Pane (Step Run 1) //ControlHost.add(jta); //Sample 04a:Create JScrollPane for JTextArea (Step Run 2) JScrollPane JSCPane = new JScrollPane( jta, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); ControlHost.add(JSCPane); |
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:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//Sample 01: Set Size and Position setBounds(100, 100, 500, 300); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create Text Area JTextArea jta = new JTextArea(11, 30); JScrollPane JSCPane = new JScrollPane( jta, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); ControlHost.add(JSCPane); //Sample 04c: JTextArea with with Word Wrap (Step Run 4) Font f = new Font("Verdana", Font.PLAIN, 16); jta.setFont(f); jta.setLineWrap(true); jta.setWrapStyleWord(true); |
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:
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
MainEntry.java
1 2 3 4 5 6 7 8 |
package tube.coding.examples; public class MainEntry { public static void main(String[] args) { //Sample 07: Create Instance of JFrameDemo JTextAreaExample frame = new JTextAreaExample("JTextArea Example"); frame.setVisible(true); } } |
JTextAreaExample.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package tube.coding.examples; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Font; import java.awt.HeadlessException; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class JTextAreaExample extends JFrame { public JTextAreaExample(String title) throws HeadlessException { super(title); //Sample 01: Set Size and Position setBounds(100, 100, 500, 300); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create Text Area JTextArea jta = new JTextArea(11, 30); JScrollPane JSCPane = new JScrollPane( jta, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); ControlHost.add(JSCPane); //Sample 04c: JTextArea with with Word Wrap (Step Run 4) Font f = new Font("Verdana", Font.PLAIN, 16); jta.setFont(f); jta.setLineWrap(true); jta.setWrapStyleWord(true); } } |
Categories: Swing
Tags: HORIZONTAL_SCROLLBAR_NEVER, JScrollPane, JTextArea, setLineWrap(), setWrapStyleWord