使 JScrollPane 自动一直向下滚动
我正在尝试使用 JTextArea 实现 JScrollPane。 JTextArea 正在被附加,我希望 JScrollPane 在添加更多文本时继续向下滚动。如何才能实现这一目标?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
对于(我认为是)更简单的答案,请查看: 文本区域滚动。
For (what I think is) a simpler answer check out: Text Area Scrolling.
我在这里找到了答案:
JScrollPane 和 JList 自动滚动
I found the answer here:
JScrollPane and JList auto scroll
如果您不断向其中写入数据,您可以使用:
添加新数据后。
这将自动在 JScorllPane 中一直向下滚动。
If you are constantly writing data to it you could use:
just after you add the new data.
This would automatically scroll all the way down the JScorllPane.
这是解决方案。
Here is the solution.
接受的解决方案效果很好,但仅当文本区域可编辑时,即没有
jTextArea.setEditable(false)
。 Krigath 建议的解决方案更通用,但存在此处提出的问题JScrollPane 和 JList 自动滚动。使用该问题的答案,您可以获得通用的解决方案,例如:仅当垂直滚动条扩展时,窗格才会向下滚动,以响应附加的文本行。
我承认可以找到一种无需额外变量即可过滤事件的方法,如果有人发布它,我将不胜感激。
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.: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.
解决方法是可能的:您可以将该侦听器声明为一个类,然后在需要它的事件上实例化它。之后,您可以在强制重新绘制屏幕后删除该类。就像魅力一样。
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.
我查看了答案,发现@user9999 答案对于那些希望滚动条连续滚动的人来说是一个很好的解决方案。我编辑了代码,去掉了变量。当用户手动滚动时,它会使滚动停止。如果用户滚动到文本区域或滚动区域的末尾,自动滚动将继续。
(正如 @user9999 建议我删除该变量并添加
jScrollPane1.getHeight()
作为测量值,以便在当前滚动条值低于最大值时停止滚动)<强>这是解决方法:
编辑:
将
-20
添加到-jScrollPane1.getHeight() - 20
,因为有时没有它就不会滚动,我猜测-20
可以根据 TextArea 的字体大小进行更改。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:
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.如果要在多线程程序中使用自动滚动,请务必小心。
例如,如果某个地方有一个类似的方法
,如果从另一个线程调用 addNewLine (或 Vertical.setValue(...)) 方法,您将得到以下异常。 (特别是,如果您尝试调整窗口大小或在启用自动滚动的情况下尝试滚动)
原因是,该方法的多线程调用扰乱了 Swings 的事件处理,因此您将得到随机结果或就像上面的错误(阅读更多这里)。
正确的方法是调用 SwingUtilities.invokeLater(...):
这样您就可以线程安全地自动滚动!
Be careful if you're about to use auto scroll within a multithreaded program.
Like for example if somewhere is a method like
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)
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(...):
This way you'll able to auto scroll threadsafe!