Java 滚动 JScrollPane,其中 JPanel 位于底部
我有一个 JScrollPane,里面有一个非常高的 JPanel,它是动态更改的,项目附加在其末尾。我想要的是滚动到上述 JScrollPane 的底部,以便新附加的项目在添加时立即可见(它们不是直接附加到滚动窗格,而是附加到其 JPanel,并且是私有对象,因此不能 我怎样才能简单地
将滚动窗格滚动到最底部? 提前致谢!
I have a JScrollPane with a very high JPanel inside, that is changed dynamically, items being appended at its end. What I want, is to scroll to the bottom of aforementioned JScrollPane in order for the newly appended items to be visible instantly on addition (they are not appended to the scroll pane directly, but to its JPanel, and are private objects, so cannot be referenced.
How can I simply have that scroll pane scroll to the very bottom?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
JComponent.scrollRectToVisible(矩形)
。在JPanel
实例上调用它。EG
屏幕截图
EG 2
此示例基于 Vincent 的 答案,使用
JScrollPane.getVerticalScrollBar()
。setValue(height)
。其中height
是面板的首选高度(以像素为单位)。JComponent.scrollRectToVisible(Rectangle)
. Call that on theJPanel
instance.E.G.
Screen shot
E.G. 2
This e.g. is based on Vincent's answer, to use
JScrollPane.getVerticalScrollBar()
.setValue(height)
. Whereheight
is the preferred height of the panel in pixels.scrollRectToVisible(...) 和scrollBar.setValue(...) 是通用的解决方案。
您可能对滚动表单感兴趣,它可以确保当当您使用 Tab 键切换到某个组件时,表单将自动滚动以确保该组件在滚动窗格中可见。它在幕后使用scrollRectToVisible()。
scrollRectToVisible(...) and scrollBar.setValue(...) are the general solutions.
You may be interested in Scrolling a Form which ensures that when you tab to a component the form will scroll automatically to make sure the the component will be visible in the scrollpane. Behind the scenes it uses scrollRectToVisible().
将滚动条一直移动到底部的一个简单方法是将其值设置为 100,如下所示:
这会使其移动到视口的底部。您可以在将组件添加到面板后添加它。
A simple way to move the scrollbar all the way to the bottom is to set its value to 100 like this:
This causes it to move to the bottom of the viewport. You can add this after the component is added to the panel.
这就是我以编程方式向下滚动的方式。
我喜欢它相当平滑地滚动到底部,而不是立即跳到那里。
This is how I scroll down programmatically.
I like how it scrolls to the bottom rather smoothly instead of jumping there immediately.