java - SwingWorker done() method seemingly not called? SwingWork not cancelled or blocked either though, Not using publish/process methods -


my question: why swingworker, seems execute tool in it's thread, , exit fine doinbackground() method (no use of cancel() swingworker method), not call done() method progress bar , label may cleaned when tool done in doinbackground()?

i'm pretty new swingworkers, i'm picking them , make lot of sense when needing run threaded processes , still update gui. ended finding swingwork class when needed add progress bar wizard-like gui. gui collects data user, , runs tool have tested , have working on file based on data.

i have basic swingworker calls tool(which called it's own thread):

private class deidtask extends swingworker<void, void> {      private deidentifier deidentifier;     boolean processing = true;      deidtask(deidentifier deid) {         this.deidentifier = deid;     }      /*      * main task. executed in background thread.      */     @override     protected void doinbackground() throws exception {         // initialize progress         setprogress(0);         //sleep @ least 1 second simulate "startup".         try {             thread.sleep(1000 + new random().nextint(2000));         } catch (interruptedexception ignore) {}         // processing         thread procthread = new thread(new deidentifierprocessthread(deidentifier));         procthread.start();         while(procthread.isalive()) {             try {                 // try join every half second                 procthread.join(500);                 setprogress(deidentifier.getprocessprogress());             } catch (interruptedexception e1) {                 e1.printstacktrace();             }                        }          processing = false;          // output flushing         thread outputthread = new thread(new deidentifieroutputthread(deidentifier));         outputthread.start();         while(outputthread.isalive()) {             try {                 // try join every quarter second                 procthread.join(250);                 setprogress(deidentifier.getoutputprogress());             } catch (interruptedexception e1) {                 e1.printstacktrace();             }          }          return null;     }      /*      * executed in event dispatching thread      */     @override     protected void done() {         lblprogress_deid.settext("done.");         progressbar_deid.setstringpainted(false);         progressbar_deid.setindeterminate(false);         button_65.setenabled(true);     } } 

please note swingworker class only implements doinbackground() , done() method. not in way call cancel(), know have ability execute done() method before doinbackground() method done.

my thought maybe swingwork wasn't executed , done() method couldn't find event dispatch thread. found such answer here, way answerer answered question simplistic more complex gui , doesn't explain how creating jframe in main method makes swing application swingwork explicitly connected to. doesn't seem me need special connect swingworker swing components.

i have created functioning gui, , place gui calls swingworker jbutton:

btnstartdeidentification = new jbutton("start de-identification");     btnstartdeidentification.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             // disable clicking of button             progressbar_deid.setindeterminate(true);             btnstartdeidentification.setenabled(false);              task_deid = new deidtask(deidentifier);             task_deid.addpropertychangelistener(new propertychangelistener() {                  @override                 public void propertychange(propertychangeevent evt) {                     int progress = task_deid.getprogress();                     system.out.println("progress is: " + progress);                     progressbar_deid.setindeterminate(false);                      progressbar_deid.setstring(null);                     progressbar_deid.setvalue(progress);                     progressbar_deid.setstringpainted(true);                     lblprogress_deid.settext("processing records (current/total): " + deidentifier.getcurrentlinenumber() + "/" + deidentifier.getnumberoflinesinfile());                  }              });             task_deid.execute();         }     });      btnstartdeidentification.setbounds(107, 195, 200, 29);     panel_rundeidtool_23.add(btnstartdeidentification); 

my next thought maybe because initialize , execute swingwork task_deid inside actionperformed() method done() method reason not finding edt. question here on so, named own question, has example of swingworker being called inside changelistener(). , answerer states that changelistener() part of edt. haven't worked changelistener in gui yet, take mean creating , calling task_deid inside actionperformed method okay do.

now, these code pieces nestled in entire class called gui have created eclipse window builder tool. gist of class is:

public class gui { private jframe frmtoolwizard; private jpanel panel_steps; ... private jpanel panel_rundeidtool_23;  ... private jbutton btnstartdeidentification; private jprogressbar progressbar_deid; private jlabel lblprogress_deid; ... private deidtask task_deid;  /**  * launch application.  */ public static void main(string[] args) {     eventqueue.invokelater(new runnable() {         public void run() {             try {                 gui window = new gui();                 window.frmtoolwizard.setvisible(true);             } catch (exception e) {                 e.printstacktrace();             }         }     }); }  /**  * create application.  */ public gui() {     initialize(); }  /**  * initialize contents of frame.  */ private void initialize() {     frmtoolwizard = new jframe();     ...     panel_steps = new jpanel();     frmtoolwizard.getcontentpane().add(panel_steps);     ...     panel_rundeidtool_23 = new jpanel();     panel_steps.add(panel_rundeidtool_23, "name_1406678727369229000");     ...     // lot of other components here     ...     btnstartdeidentification = new jbutton("start de-identification");     ... } private class deidtask extends swingworker<void, void> { ... } 

}

the whole point of calling done() method want finalize progress bar, make blank, , set label under progress bar "done." end user knows can move on. of want seen in deidtask done() method.

any appreciated. if obvious answer apologize in advance, because not seeing need fix problem.

isdone() called after doinbackground() receive property change event after completion of isdone() , since listener overwrites ui properties modifying in isdone() drawing wrong conclusion.

propertychangeevents sent different properties, notably progress , state. last event receive after isdone has been called telling state property of swingworker went started done.

you can examine property name of propertychangeevent tell changes of progress , state apart.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -