Making a JScrollPane automatically scroll all the way down
Solution 1
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!
Solution 2
I found the answer here: JScrollPane and JList auto scroll
scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
e.getAdjustable().setValue(e.getAdjustable().getMaximum());
}
});
Solution 3
If you are constantly writing data to it you could use:
textArea.setCaretPosition(textArea.getDocument().getLength());
just after you add the new data.
This would automatically scroll all the way down the JScorllPane.
Solution 4
Here is the solution.
JTextArea textArea = new JTextArea();
DefaultCaret caret = (DefaultCaret)textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);`
Solution 5
The accepted solution works good, but only when the text area is editable, i.e. without jTextArea.setEditable(false)
. The solution suggested by Krigath is more general, but has the problem as asked here JScrollPane and JList auto scroll. Using answers from that question you can get general solution, e.g.:
JScrollPane scrollPane = new JScrollPane(jTextArea);
verticalScrollBarMaximumValue = scrollPane.getVerticalScrollBar().getMaximum();
scrollPane.getVerticalScrollBar().addAdjustmentListener(
e -> {
if ((verticalScrollBarMaximumValue - e.getAdjustable().getMaximum()) == 0)
return;
e.getAdjustable().setValue(e.getAdjustable().getMaximum());
verticalScrollBarMaximumValue = scrollPane.getVerticalScrollBar().getMaximum();
});
The Pane then is scrolled down only when vertical scroll bar is expanding, in response to appended lines of text.
I admit that that a method to filter events without extra variables could be found, and would appreciate if somebody post it.
Krigath
Updated on December 26, 2021Comments
-
Krigath over 2 years
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?
-
trashgod about 14 years+1 The
setUpdatePolicy()
API explains so much. I've updated my answer to the cited question accordingly. -
jackrabbit about 14 yearsYour "solution" is actually the problem of the linked-to question: if you manually try to move the vertical scroll handle, this code will scroll to the bottom again!
-
camickr about 14 yearsYes, the API keeps changing its hard to keep up with changes all the time.
-
kleopatra over 11 yearswhile possible, it's not the best solution - which is @camickr 's answer :-)
-
jcalfee314 over 10 yearsThis helps, but if the users clicks anywhere other than after the end of the last line the auto scrolling stops. This is not intuitive. It would be nice if the usual behaviour worked, just by scrolling to the bottom the auto-scroll is reactivated.
-
camickr over 10 years@jcalfee314, Check out Smart Scrolling for another potential solution.
-
kleopatra over 10 years+1 for the correct answer - even though it's the same as the Rob's accepted answer :-)
-
Rok T. over 6 years@jcalfee314 To fix this, I added a button which, when clicked, enabled auto scrolling again. The code for the button only needs this: textArea.setCaretPosition(textArea.getDocument().getLength());
-
Krisztian Nagy Zsolt over 2 yearsI edited your code and it works quite well. +1
-
Krisztian Nagy Zsolt over 2 years@jcalfee314 Here is an answer for that issue .
-
Krisztian Nagy Zsolt over 2 years@jackrabbit Here is a code to make it stop scrolling if the user manually moves the vertical scrollbar. It continues to scroll if the user scrolls to the bottom.