Making a JScrollPane automatically scroll all the way down

后端 未结 6 1348
夕颜
夕颜 2021-02-05 07:42

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 thi

相关标签:
6条回答
  • 2021-02-05 07:53

    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.

    0 讨论(0)
  • 2021-02-05 07:56

    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.

    0 讨论(0)
  • 2021-02-05 08:05

    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.

    0 讨论(0)
  • 2021-02-05 08:07

    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!

    0 讨论(0)
  • 2021-02-05 08:11

    Here is the solution.

    JTextArea textArea = new JTextArea();
    DefaultCaret caret = (DefaultCaret)textArea.getCaret();
    caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);` 
    
    0 讨论(0)
  • 2021-02-05 08:14

    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());  
            }
        });
    
    0 讨论(0)
提交回复
热议问题