java - How to stop a software with a timer repeat over and over? -


i have created software timer , when goes 0 it's going start new login screen. problem login comes on , on again. how stop this?

class displaycountdown extends timertask {      int seconds = 0005;      public void run() {         if (seconds > 0) {             int hr = (int) (seconds / 3600);             int rem = (int) (seconds % 3600);             int mn = rem / 60;             int sec = rem % 60;             string hrstr = (hr < 10 ? "0" : "") + hr;             string mnstr = (mn < 10 ? "0" : "") + mn;             string secstr = (sec < 10 ? "0" : "") + sec;             seconds--;             lab.settext(hrstr + " : " + mnstr + " : " + secstr + "");         } else {             login ty = new login();             login.scname.settext(scname.gettext());             login.scnum.settext(scnum.gettext());             login.mar.settext(jtextfield1.gettext());             ty.setvisible(true);             dispose();         }     } } 

this violating single thread rules of swing - updating ui out side context of event dispatching thread.

instead of timertask, should using javax.swing.timer.

javax.swing.timer swingtimer = new javax.swing.timer(500, new actionlistener() {     public void actionperformed(actionevent evt) {         if (seconds > 0) {             //...         } else {             ((javax.swing.timer)evt.getsource()).stop();             //...         }     } }); 

take @ concurrency in swing


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -