Assuming the setup as outlined in my comment to your question, the basic approach is to compare the parent's (the component that contains the 3 panels) visibleRect with its children's bounds. Something like:
final JComponent parent = new JPanel(); // new PageScrollable();
parent.setLayout(new BoxLayout(parent, BoxLayout.PAGE_AXIS));
Color[] color = new Color[] {Color.YELLOW, Color.RED, Color.GREEN};
for (int i = 0; i < color.length; i++) {
JTable table = new JTable(10, 5);
// color it to see some difference
table.setBackground(color[i]);
// set a name for logging
table.setName("table at: " + i);
parent.add(table);
}
JScrollPane scrollPane = new JScrollPane(parent);
Action visible = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
Rectangle rect = parent.getVisibleRect();
for (int i = 0; i < parent.getComponentCount(); i++) {
// implement logic as needed to compare the parent's visible rect
// with the children's bounds
if (rect.intersects(parent.getComponent(i).getBounds())) {
System.out.println("found: " + parent.getComponent(i).getName());
}
}
}
};
frame.add(scrollPane);
frame.add(new JButton(visible), BorderLayout.SOUTH);
As an aside: to fine-tune scrolling behaviour you might consider a custom panel which implements Scrollable, something like:
/**
* Implement a panel of type Scrollable to fine-tune its scrolling behaviour.
* This implements the prefScrollableSize to the prefSize of the first child
* and both block/unit increment to the height of the prefScrollable.
*/
public static class PageScrollable extends JPanel implements Scrollable {
@Override
public Dimension getPreferredScrollableViewportSize() {
if (getComponentCount() > 0) {
return getComponent(0).getPreferredSize();
}
return super.getPreferredSize();
}
@Override
public int getScrollableUnitIncrement(Rectangle visibleRect,
int orientation, int direction) {
return getPreferredScrollableViewportSize().height;
}
@Override
public int getScrollableBlockIncrement(Rectangle visibleRect,
int orientation, int direction) {
return getPreferredScrollableViewportSize().height;
}
@Override
public boolean getScrollableTracksViewportWidth() {
return false;
}
@Override
public boolean getScrollableTracksViewportHeight() {
return false;
}
}