Handling timers in java

左心房为你撑大大i 提交于 2019-12-13 05:34:25

问题


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.



来源:https://stackoverflow.com/questions/5777931/handling-timers-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!