java - Simple Swing Delay -


in swing application, have popup jdialog pops jlabel says "hang on 5 seconds."

after 5 seconds, label should change "okay, i'm done." , button should appear allowing user click continue.

in example action below (linked button causes popup), popup appears should blank instead of saying "hang on 5 seconds." after 5 seconds updates , labels there , button too. what's going on? thread sleeping before repaint or something?

@action     public void popup() {         popupdialog.setsize(300,200);         popupdialog.setlocationrelativeto(null);         popupdialog.setvisible(true);          popuplabel.setvisible(true);         popuplabel.settext("working, hang on sec....");          try {             thread.sleep(5000);         } catch (interruptedexception ex) {             thread.currentthread().interrupt();         }          popuplabel.settext("okay i'm done.");         popupbut.setvisible(true);     } 

edit: tried this, in effort use swing timer in place of thread sleeping:

@action         public void popup() {             popupdialog.setsize(300,200);             popupdialog.setlocationrelativeto(null);             popupdialog.setvisible(true);              popuplabel.setvisible(true);             popuplabel.settext("working, hang on sec....");              timer timer = new timer(speed, this);             timer.setinitialdelay(pause);             timer.start();               popuplabel.settext("okay i'm done.");             popupbut.setvisible(true);         } 

obviously i'll need more code finish timer, right off bat symbol not found, variable: timer error. what's about? doing wrong?

edit 2: changed timer declaration , solved 1 problem created another. getting symbol not found error in regards speed. have never used swing timer before , don't know how use them. java tutorial on topic convoluted , difficult understand. can of point me simple, clear example of timer can learn , figure out need do?

if code modifying swing component's state on edt here (it should be), no repainting of label first text take place if call repaint(), because other edt requests queued before last repaint need complete before reach repaint, , code here 1 of queued edt events.

if call repaint, adds repaint queue, doesn't repaint right away. actions here result in 5 second wait, label before next repaint having text last set (as code queued on edt executed before going next queued).

try using swing timer, events fired swing timer executing on edt, it's pretty need, 1 event here set text initially, , event fired swing timer change text after 5 seconds.

edit, example of swing timer firing once, after 5 seconds requested author:

    // set first jlabel text here      actionlistener task = new actionlistener() {         public void actionperformed(actionevent e) {             system.out.println("this on edt after 5 seconds, " +                 "well depending on if i'm used timer, , if " +                 "the right options set timer");             // set second jlabel text here         }         };     timer timer = new timer(5000 , task);     timer.setrepeats(false);     timer.start(); 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Website Login Issue developed in magento -

Can the constants be defined inside a model file of a framework in PHP? -