Java 使 JScrollPane 自动一直向下滚动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2483572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making a JScrollPane automatically scroll all the way down
提问by Krigath
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?
我正在尝试使用 JTextArea 实现 JScrollPane。JTextArea 被附加到,我希望 JScrollPane 在添加更多文本时继续向下滚动。如何做到这一点?
采纳答案by camickr
For (what I think is) a simpler answer check out: Text Area Scrolling.
对于(我认为是)一个更简单的答案,请查看: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!
在 JDK5 之前,您必须在每次追加后手动更改插入符号的位置。您现在可以将此行为作为默认设置,如下所示:
JTextArea textArea = new JTextArea(); DefaultCaret caret = (DefaultCaret)textArea.getCaret(); caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
这样做的好处是你不需要在你的代码中多次使用这个片段!
回答by Krigath
I found the answer here: JScrollPane and JList auto scroll
我在这里找到了答案: JScrollPane and JList auto scroll
scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
e.getAdjustable().setValue(e.getAdjustable().getMaximum());
}
});
回答by 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.
一种解决方法是可能的:您可以将该侦听器声明为一个类,然后在需要它的事件上将其实例化。之后,您可以在强制重新绘制屏幕后删除该类。奇迹般有效。
回答by 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.
这将自动向下滚动到 JScorllPane。
回答by Jeevan B. Manoj
Here is the solution.
这是解决方案。
JTextArea textArea = new JTextArea();
DefaultCaret caret = (DefaultCaret)textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);`
回答by user9999
The accepted solution works good, but only when the text area is editable, i.e. without jTextArea.setEditable(false)
. The solution suggested by Krigathis 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.:
接受的解决方案效果很好,但仅当文本区域可编辑时,即没有jTextArea.setEditable(false)
. Krigath建议的解决方案更通用,但存在JScrollPane 和 JList auto scroll此处所问的问题。使用该问题的答案,您可以获得通用解决方案,例如:
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.
我承认可以找到一种无需额外变量即可过滤事件的方法,如果有人发布它,我将不胜感激。