Making a JScrollPane automatically scroll all the way down

拜拜、爱过 提交于 2019-12-03 02:49:30
camickr

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!

Krigath

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());  
        }
    });
Harold Martin

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.

Jeevan B. Manoj

Here is the solution.

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

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.

My solution

(PS: Edit for SCREEN SHOOT)
import javax.swing.*;
import javax.swing.text.DefaultCaret;

public class NewClass {

    public static void main(String[] args) {
        JFrame win = new JFrame("test");
        win.setSize(800, 500);
        win.setResizable(false);
        win.setLayout(null);
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        win.setLocationRelativeTo(null);

        JTextArea txtListNum = new JTextArea();
        JScrollPane barList = new JScrollPane(txtListNum);
        DefaultCaret crList = (DefaultCaret) txtListNum.getCaret();
        crList.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

        barList.setLocation(10, 10);  // Scrollbar Position
        barList.setSize(180, 450);   // Scrollbar Dimession
        win.add(barList);            // Scrolbar add to Frame

        win.setVisible(true);

    }

}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!