68

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Amit
  • 33,847
  • 91
  • 226
  • 299

8 Answers8

151

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

textArea.append(...);
textArea.setCaretPosition(textArea.getDocument().getLength());

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();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

Note:

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

camickr
  • 321,443
  • 19
  • 166
  • 288
  • @camickr, i can't access the link, I am sitting behind a corporate firewall. Can you post significant stuff from that link,a nd I will up vote you:) Thanks. – Suraj Chandran Oct 27 '09 at 03:42
  • 1
    Any ideas why this might not be working (for what it's worth the form was created using the Netbeans builder)? – cyber-monk Jul 15 '13 at 22:02
  • @cyber-monk, No. Create a SSCCE. If it still doesn't work then you can create a question and post your SSCCE. – camickr Jul 16 '13 at 00:19
  • 1
    I removed your side note from your answer - if this is still an issue, post something on [meta]. – Duncan Jones Nov 22 '13 at 16:35
  • 2
    According to the article on text area scrolling referenced in your answer, setting the caret update policy no longer works. It's likely best to stick with setting the caret position explicitly. – jstricker Jul 13 '16 at 18:24
  • Second example doesn't seem to work in JDK8, or at least doesn't scroll consistently when text is appended, though I haven't dug into why. First example works flawlessly. – Hasan Aslam Jan 23 '21 at 16:55
  • I found [another solution](https://stackoverflow.com/a/70479122/7269325) to the problem. The scrolling stops when the user is scrolling manually and continues after the user scrolls to the bottom of textarea. – Krisztian Nagy Zsolt Dec 25 '21 at 09:43
  • `caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE)` is not working for me? – Cardinal System Nov 09 '22 at 16:34
2
    JScrollBar vbar = scrollPane.getVerticalScrollBar();

    for (int i = 0; i < 20; i++) {

        myJTxt.append("This is text " + i + "\n");
        vbar.setValue(vbar.getMaximum());
        vbar.paint(vbar.getGraphics());
        myJTxt.scrollRectToVisible(myJTxt.getVisibleRect());
        myJTxt.paint(myJTxt.getGraphics());
        try {
            Thread.sleep(250);
        } catch (InterruptedException ex) {
            Logger.getLogger(ScrollTextView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
user211233
  • 31
  • 1
2

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.

ton1
  • 7,238
  • 18
  • 71
  • 126
2
    JTextArea jTextArea = new JTextArea();
    DefaultCaret caret = (DefaultCaret)jTextArea.getCaret();
    caret.setUpdatePolicy(DefaultCaret.OUT_BOTTOM);
kayz1
  • 7,260
  • 3
  • 53
  • 56
2

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.

HAig
  • 21
  • 1
  • 2
0

Best and easiest way, Try this :

  import javax.swing.text.DefaultCaret;
  DefaultCaret caret = (DefaultCaret) textArea.getCaret();
  caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
Dilan
  • 2,610
  • 7
  • 23
  • 33
-2

Use this instead

JTextArea textArea = new JTextArea();
DefaultCaret caret = (DefaultCaret)textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
scrollPane = new JScrollPane();
scrollPane.add(textArea);
scrollPane.setViewportView(textArea);
Pshemo
  • 122,468
  • 25
  • 185
  • 269
-3

Try this:

JTextArea jTextArea = new JTextArea();
JScrollPane jScrollPane = new JScrollPane();
jScrollPane.setViewport(jTextArea);
sam1370
  • 335
  • 2
  • 18
Honest
  • 182
  • 11