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

8

70

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.

Araarab answered 26/10, 2009 at 20:4 Comment(0)
H
153

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

Heraclitus answered 26/10, 2009 at 20:10 Comment(8)
@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.Taima
Any ideas why this might not be working (for what it's worth the form was created using the Netbeans builder)?Endoskeleton
@cyber-monk, No. Create a SSCCE. If it still doesn't work then you can create a question and post your SSCCE.Heraclitus
I removed your side note from your answer - if this is still an issue, post something on Meta Stack Overflow.Tetrad
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.Albuminoid
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.Ingraft
I found another solution to the problem. The scrolling stops when the user is scrolling manually and continues after the user scrolls to the bottom of textarea.Waldron
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE) is not working for me?Deyo
O
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);
        }
    }
Oneirocritic answered 15/11, 2009 at 0:26 Comment(0)
C
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.

Chanel answered 21/12, 2013 at 19:59 Comment(0)
P
2
    JTextArea jTextArea = new JTextArea();
    DefaultCaret caret = (DefaultCaret)jTextArea.getCaret();
    caret.setUpdatePolicy(DefaultCaret.OUT_BOTTOM);
Pennant answered 17/12, 2014 at 8:39 Comment(0)
N
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.

Nehemiah answered 27/1, 2019 at 16:37 Comment(0)
B
0

Best and easiest way, Try this :

  import javax.swing.text.DefaultCaret;
  DefaultCaret caret = (DefaultCaret) textArea.getCaret();
  caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
Bryannabryansk answered 8/9, 2020 at 4:7 Comment(0)
O
-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);
Overunder answered 17/12, 2014 at 10:32 Comment(0)
L
-3

Try this:

JTextArea jTextArea = new JTextArea();
JScrollPane jScrollPane = new JScrollPane();
jScrollPane.setViewport(jTextArea);
Lynelllynelle answered 14/11, 2013 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.