Making a JScrollPane automatically scroll all the way down

Krigath picture Krigath · Mar 20, 2010 · Viewed 54.2k times · Source

I am trying to implement a JScrollPane with a JTextArea. The JTextArea is being appended to, and I want the JScrollPane to keep scrolling down as more text is added. How can this be achieved?

Answer

camickr picture camickr · Mar 20, 2010

For (what I think is) a simpler answer check out: Text Area Scrolling.

Prior to JDK5, you would have to manually change the caret's position after each append. You can now give this behaviour as a default like this :

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

The advantage of this is that you don't need to use this snippet more than once in your code!