Making a JScrollPane automatically scroll all the way down

后端 未结 6 1351
夕颜
夕颜 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 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!

提交回复
热议问题