Get shown component in JScrollPane

南楼画角 提交于 2019-12-05 10:13:57

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 Rectangle

4) and Rectangle#intersects returns Boolean value if is JComponent(s) visible or not in JViewPort

kleopatra

As already commented to @mKorbel's answer:

  • it's correct that you need the child bounds
  • it's correct that you need to intersect those bounds with "something"
  • it's wrong that you need the containing viewport (nor the scrollpane)

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:

Rectangle visibleRect = myPanel.getVisibleRect();
for (Component child : myPanel.getComponents()) {
   Rectangle childBounds = child.getBounds();
   if (childBounds.intersects(visibleRect)) {
       // do stuff
   }
}

How about asking the components if they're visible:

for ( Component component : jpanel.getComponents() ) {
    if ( component instanceof JButton && component.isShowing() ) {
        // We've found a button that is showing...
    }
}
scrollPane.getViewport().getView()
scrollPane.getViewport().getViewRect()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!