获取 JScrollPane 中显示的组件
我有一个包含 JPanel 的 JScrollPane。我用许多按钮填充了这个 JPanel。
是否有可能获取当前显示的按钮?
我知道我可以通过 jpanel.getComponents() 访问 JPanel 的子级,但这些都是此窗格中的组件;我只想要当前屏幕上的那些。
I have a JScrollPane containing a JPanel. I fill this JPanel with many buttons.
Is there any possibility to get the currently shown buttons?
I know I can access the children of a JPanel via jpanel.getComponents()
but those are all components in this pane; I want only the ones that are currently on screen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如已经对 @mKorbel 的回答所评论的那样:
您需要包含视口(也不是滚动窗格)的JComponent 有一个 API 可以独立于当前显示方式/位置来获取当前可见部分,因此“某物”是 JComponent 的visibleRect:
As already commented to @mKorbel's answer:
JComponents have an API to get their currently visible part independently of how/where exactly they are currently shown, so the "something" is the JComponent's visibleRect:
我假设这个容器已经在屏幕上可见,那么我建议
1) 提取 JViewPort 来自 JScrollPane,
2) addChangeListener 到
JViewPort
3) 每个可见的
JComponent(s)
返回 矩形4) 和 Rectangle#intersects 返回
Boolean
值,判断JComponent(s)
在JViewPort
中是否可见I assume that this container is already visible on the screen, then I suggest
1) to extract JViewPort from JScrollPane,
2) addChangeListener to
JViewPort
3) each visible
JComponent(s)
returns Rectangle4) and Rectangle#intersects returns
Boolean
value if isJComponent(s)
visible or not inJViewPort
询问组件是否可见如何:
Component#isShowing()
How about asking the components if they're visible:
Component#isShowing()