在Swing应用程序中,我想监视是否与顶级容器的子组件发生了任何交互。是否有任何内置的Swing机制可以帮助我完成此任务?
我的问题是,如果用户有一段时间没有与之交互,我想关闭该容器。但是,我想避免对所有组件(例如特定的侦听器)添加检测。有可能吗?
public static void addInactivityTimeout(final Container c, final long timeout) {
final Timer t = new Timer(c.getName() + INACTIVITY_SUFFIX);
t.schedule(new TimerTask() {
private long inactivityTime = 0;
@Override
public void run() {
if (!hadInteraction(c)) {
inactivityTime += INACTIVITY_POLLING_PERIOD;
if (inactivityTime > timeout) {
c.setVisible(false);
}
} else {
inactivityTime = 0;
}
t.cancel();
}
}, INACTIVITY_POLLING_PERIOD,
INACTIVITY_POLLING_PERIOD);
}
public static boolean hadInteraction(Container c) {
/**
* Is there any method in Swing to help writing this
* function without adding extras instrumentation
* throughout the code ?
*/
}