Making a JScrollPane automatically scroll all the way down
Asked Answered
D

8

23

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?

Discontinuity answered 20/3, 2010 at 15:23 Comment(0)
K
41

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!

Kazukokb answered 20/3, 2010 at 16:32 Comment(6)
+1 The setUpdatePolicy() API explains so much. I've updated my answer to the cited question accordingly.Implosion
Yes, the API keeps changing its hard to keep up with changes all the time.Kazukokb
This 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.Asbestos
@jcalfee314, Check out Smart Scrolling for another potential solution.Kazukokb
@Asbestos 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());Luzern
@Asbestos Here is an answer for that issue .Hachmann
D
9

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());  
        }
    });
Discontinuity answered 20/3, 2010 at 15:38 Comment(2)
Your "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!Chem
@Chem 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.Hachmann
S
7

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.

Skutchan answered 14/7, 2012 at 0:45 Comment(1)
while possible, it's not the best solution - which is @Kazukokb 's answer :-)Brok
L
5

Here is the solution.

JTextArea textArea = new JTextArea();
DefaultCaret caret = (DefaultCaret)textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);` 
Leukas answered 4/1, 2014 at 12:7 Comment(1)
+1 for the correct answer - even though it's the same as the Rob's accepted answer :-)Brok
M
2

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.

Mandal answered 27/9, 2018 at 15:6 Comment(1)
I edited your code and it works quite well. +1Hachmann
C
1

A work around is possible: you can declare that listener as a class then instantiate it on the event where it is needed. After which you can remove the class after forcing a repaint of the screen. Works like a charm.

Cartie answered 9/9, 2011 at 1:44 Comment(0)
H
0

I was look at the answers and found that @user9999 answer is a good solution for those who want the scrollbar continuously scroll. I edited the code, got rid of the variable. It make the scrolling stop - when the user is scrolling manually. If the user scrolls to the end of the textarea or scrollarea, the auto scrolling continues.

(also as @user9999 suggested i removed the variable and added the jScrollPane1.getHeight() as the measure value to stop scrolling if the current scrollbar value is lower than max)

Here is the workaround:

jScrollPane1.getVerticalScrollBar().addAdjustmentListener(
e -> {
    if ((e.getAdjustable().getValue() - e.getAdjustable().getMaximum()) > -jScrollPane1.getHeight() - 20){
        e.getAdjustable().setValue(e.getAdjustable().getMaximum());
    }   
});

Edit:

Added -20 to the -jScrollPane1.getHeight() - 20 as it is sometimes doesnt scroll without it, i guess the -20 can be changed depends on the font size of the TextArea.

Hachmann answered 25/12, 2021 at 9:33 Comment(0)
P
0

Be careful if you're about to use auto scroll within a multithreaded program. Like for example if somewhere is a method like

public void addNewLine(String s){
        textPane.setText(textPane.getText()+"\n"+s);
        if(autoscrollCheckBox.isSelected()){
            this.revalidate();
            JScrollBar vertical = scrollPane.getVerticalScrollBar();
            vertical.setValue(vertical.getMaximum());
        }
}

You will get the following exception, if the addNewLine (or the vertical.setValue(...)) method is called from another thread. (Especially, if you're try to resize the window or try to scroll while, autoscroll is enabled)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.text.BoxView.calculateMajorAxisRequirements(BoxView.java:871)
at javax.swing.text.BoxView.checkRequests(BoxView.java:930)
at javax.swing.text.BoxView.getMinimumSpan(BoxView.java:568)
...

The reason for this is, the multithreaded call of the method is messing up with Swings' eventhandling, so you'll get random results or like above an error (read morehere).

The correct way is to call SwingUtilities.invokeLater(...):

public void addNewLine(String s){
       SwingUtilities.invokeLater( () -> {
            textPaneOutput.setText(textPaneOutput.getText()+"\n"+s);
            if(autoscrollCheckBox.isSelected()){
                this.revalidate();
                JScrollBar vertical = scrollPane.getVerticalScrollBar();
                vertical.setValue(vertical.getMaximum());
            }
        });
 }

This way you'll able to auto scroll threadsafe!

Pass answered 2/9, 2022 at 8:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.