I have an application which runs a timer to check for idle time and once there is no activity for 10 seconds the application will close. I have nearly 100 screens and i want to track the inactivity seconds on all the screens. Its hard for me to write the handling events in all buttons, textboxes, labelboses one by one. What i have to do is add 10 seconds on every action of the user on the application. Even if it is mousemove add 10 seconds so tat the application wont close for another 10 seconds. Is there any way to handle this effectively ?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
I would suggest the following handler:
final Timer tm = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("10 SECONDS AND NOTHING HAPPENED");
}
});
tm.start();
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
@Override
public void eventDispatched(AWTEvent event) {
tm.restart();
}
}, -1);
回答2:
You could look into Toolkit.addAWTEventListener this allows you to add a MouseMotionListener to react to mouse movements throughout your app and act accordingly.