Imagine you have a JScrollPane
and many JLabel
s or any other JComponent
s 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();
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 :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With