java - Encountering problems with Thread.sleep in Swing -


this program written count 0 1000 goes straight 1000 without displaying counting process. have written similar code using progress bar , thread.sleep() method , works perfectly.

import javax.swing.jframe; import javax.swing.jbutton; import javax.swing.jtextfield; import javax.swing.jpanel; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener;  public class project extends jframe implements actionlistener {      jbutton countupbutton = new jbutton("count up");     jbutton countdownbutton = new jbutton("count down");     jbutton resetbutton = new jbutton("reset");     jtextfield numberfield = new jtextfield();     int count = 0;      public project(){         setlayout(new gridlayout(1, 4));         setsize(500, 300);         add(numberfield);         add(countupbutton);         add(countdownbutton);         add(resetbutton);         countupbutton.addactionlistener(this);         countdownbutton.addactionlistener(this);         resetbutton.addactionlistener(this);         numberfield.settext("0");         setvisible(true);         setdefaultcloseoperation(exit_on_close);      }      @override     public void actionperformed(actionevent a){         if(countupbutton.hasfocus()){                     count = integer.parseint(numberfield.gettext());             try{             while(count < 1000){                 count = count + 1;                 numberfield.settext(integer.tostring(count));                 thread.sleep(100);                             }             }catch(interruptedexception r){                 r.printstacktrace();             }         }         if(countdownbutton.hasfocus()){             count = integer.parseint(numberfield.gettext());             try{                 while(count > 0){                     count -= 1;                     numberfield.settext(integer.tobinarystring(count));                     thread.sleep(100);                                     }             }catch(interruptedexception r){                 r.printstacktrace();             }         }         if(resetbutton.hasfocus()){             numberfield.settext("0");         }     }      public static void main(string args[]){         new project();     } } 

any long-running task should run in separate thread. use of thread.sleep qualifies long-running.

by running in swing user-interface thread, no updates can rendered in user-interface until code completes. instead counting should spawned in thread. other thread should periodically update user interface in thread-safe manner using swingworker.

study on launching threads, executors such scheduledexecutorservice, swing event-dispatch thread (edt), , swingworker.

swing timer

a simpler approach might swing timer class (tutorial), not confused java.util.timer. of thread-handling work you. have no experience it.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -