How to set AUTO-SCROLLING of JTextArea in Java GUI?

JavaUser InterfaceSwingJscrollpaneJtextarea

Java Problem Overview

I have embedded a JTextArea on a JScrollPane and am using that JTextArea for output.

I want that whenever the ouput goes beyond the size of the JTextArea, the JTextArea scrolls automatically so that user don't have to do manual scroll down to see the recent output.

How can I do that?

I have already set the autoscroll property of both JTextArea and JScrollPane to true.

Java Solutions

Solution 1 - Java

When using JDK1.4.2 (or earlier) the most common suggestion you will find in the forums is to use code like the following:


However, I have just noticed that in JDK5 this issue has actually been resolved by an API change. You can now control this behaviour by setting a property on the DefaultCaret of the text area. Using this approach the code would be:

JTextArea textArea = new JTextArea();
DefaultCaret caret = (DefaultCaret)textArea.getCaret();

The above suggestion to set the caret update policy does not work.

Instead you may want to check out Smart Scrolling which gives the user the ability to determine when scrolling should be automatic or not.

A more detailed description of automatic scrolling in a text area can be found here: Text Area Scrolling

Solution 2 - Java

    JScrollBar vbar = scrollPane.getVerticalScrollBar();
    for (int i = 0; i < 20; i++) {
        myJTxt.append("This is text " + i + "\n");
        try {
        } catch (InterruptedException ex) {
            Logger.getLogger(ScrollTextView.class.getName()).log(Level.SEVERE, null, ex);

Solution 3 - Java

When you click anywhere over JTextArea, auto-scrolling have possible to be stopped. Because the position of caret once changed, view point changed too. In this time you should set caret position when you append or add some text. On my way, I made method including set caret position, and then use it when anything to be added or appended.

Solution 4 - Java

	JTextArea jTextArea = new JTextArea();
	DefaultCaret caret = (DefaultCaret)jTextArea.getCaret();

Solution 5 - Java

I tried most suggestions but ran into problems when the content of the JTextArea becomes large (several MB). Finally the following showed best performance:

myTextArea.append( someText );
myTextArea.getCaret().setDot( Integer.MAX_VALUE );

Of course, any selection the user had made gets lost. Hence, its only usable for a "display-only" usage of the text area.

Still, in my installation, if the content of the JTextArea exceeds some 9MB, it gets kind of unusable (very sluggish to almost frozen GUI).

A similar phenomenon occurs when the text contains characters that are represented by two chars (two 16-bit units) in the UTF-16 encoding (so-called surrogate pairs, like this: ). I have a solution for filtering, but maybe a different topic.

Solution 6 - Java

Best and easiest way, Try this :

  import javax.swing.text.DefaultCaret;
  DefaultCaret caret = (DefaultCaret) textArea.getCaret();

Solution 7 - Java

Use this instead

JTextArea textArea = new JTextArea();
DefaultCaret caret = (DefaultCaret)textArea.getCaret();
scrollPane = new JScrollPane();

Solution 8 - Java

Try this:

JTextArea jTextArea = new JTextArea();
JScrollPane jScrollPane = new JScrollPane();


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAmitView Question on Stackoverflow
Solution 1 - JavacamickrView Answer on Stackoverflow
Solution 2 - Javauser211233View Answer on Stackoverflow
Solution 3 - Javaton1View Answer on Stackoverflow
Solution 4 - Javakayz1View Answer on Stackoverflow
Solution 5 - JavaHAigView Answer on Stackoverflow
Solution 6 - JavaChandra_lalView Answer on Stackoverflow
Solution 7 - JavaJavaBeginnerView Answer on Stackoverflow
Solution 8 - JavaHonestView Answer on Stackoverflow