Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether JComponent inside JScrollPane is visible to a user?

Imagine you have a JScrollPane and many JLabels or any other JComponents added to it.

How would you check if a certain component is visible/partially visible/not visible to user? (scrolling)

I have tried to Google but could not find an easy solution. Is there some existing method I am missing or we have to deal with coordinates and rectangular comparison?

UPD: the following is not working in my case. It seem to relate to JLabel.setVisible(true/false) but not being inside JScrollPane

JLabel.isVisible();
like image 747
Nikolay Kuznetsov Avatar asked Nov 29 '12 14:11

Nikolay Kuznetsov


1 Answers

Have a look at JComponent java doc:

Rectangle r = child.getVisibleRect();
if (r.getSize().equals(child.getSize())) {
   // fully visible
} else if (r.isEmpty()) {
   // not visible
} else {
  // partly visible
}

Edit

changed the condition for not-visible to use Rectangle api - thanks to @mKorbel for reminding me :-)

like image 186
kleopatra Avatar answered Sep 20 '22 00:09

kleopatra