Get featured component in scrollpane

I have a scrollpane where the jpanel is located inside, and I fill this jpanel with many buttons. Is it possible to get the currently displayed components?

Because I want to know which buttons are currently displayed. (I know that I can access the jpanel children through jpanel.getComponents(), but these are all components in this area. I only need the one that is currently on the screen).

BR

Leon

+2
source share
4 answers

I assume that this container is already visible on the screen, then I suggest

1) extract JViewPort from JScrollPane ,

2) addChangeListener upJViewPort

3) JComponent(s) Rectangle

4) # Boolean , JComponent(s) JViewPort

+5

@mKorbel

  • ,
  • , "-"
  • , ( )

JComponents api, , / , "-" visibleRect:

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

, :

for ( Component component : jpanel.getComponents() ) {
    if ( component instanceof JButton && component.isShowing() ) {
        // We've found a button that is showing...
    }
}
+1
scrollPane.getViewport().getView()
scrollPane.getViewport().getViewRect()
+1

All Articles