如何检查JScrollPane中的JComponent是否对用户可见? [英] How to check whether JComponent inside JScrollPane is visible to a user?
问题描述
想象一下,您有一个JScrollPane
并添加了许多JLabel
或任何其他JComponent
.
Imagine you have a JScrollPane
and many JLabel
s or any other JComponent
s added to it.
您将如何检查某个组件是否对用户可见/部分可见/不可见?(滚动)
我曾经尝试过使用Google,但找不到简单的解决方案.是否有我缺少的现有方法,或者我们必须处理坐标和矩形比较?
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:以下情况不适用于我.它似乎与JLabel.setVisible(true/false)有关,但不在JScrollPane内部
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();
推荐答案
看看JComponent java doc:
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
}
修改
更改了不可见的使用Rectangle api的条件-感谢@mKorbel提醒我:-)
changed the condition for not-visible to use Rectangle api - thanks to @mKorbel for reminding me :-)
这篇关于如何检查JScrollPane中的JComponent是否对用户可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!