java - Indeterminate Progress bar wont show -


i have application copies files (via adb) android tablet. takes time want display popup indeterminate progress bar on it. when copy task complete want able stop progress bar , let user close dialog.

at moment have not added dialog box , trying progress bar working. problem have progress bar not showing @ start of task, dont know why. progressbar shows when dialog box saying sync complete appears. code is:

        progress = new jprogressbar(0, 100);         progress.setforeground(new color(255, 99, 71));         progress.setindeterminate(true);         progress.setvalue(0);         progress.setpreferredsize( new dimension( 300, 20 ) );         progress.setbounds( 278, 12, 260, 20 );         progress.setvisible(false);         progress.setstring("sync in progress");         progress.setstringpainted(true);         contentpane.add(progress);         pushtotab = new jbutton("");         pushtotab.addactionlistener(new actionlistener() {    public void actionperformed(actionevent arg0) {                         if (buildpathset==1){                             try{                             setcursor(cursor.getpredefinedcursor(cursor.wait_cursor));                             progress.setvisible(true);                             wiredsync();                         }finally{                             joptionpane.showmessagedialog(null, "sync complete. ",null, buildpathset);                              setcursor(cursor.getdefaultcursor());                                    progress.setvisible(false);                         }}else{      //warning in here later -                 }                 }                 });  public void wiredsync(){          try {                      process process = runtime.getruntime().exec("adb" + " push "+ buildpath + " " + adbtabletsync);                     inputstreamreader reader = new inputstreamreader(process.getinputstream());                     scanner scanner = new scanner(reader);                     scanner.close();                     int exitcode = process.waitfor();                     system.out.println("process returned: " + exitcode);                  } catch(ioexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 } catch (interruptedexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 }     }//end  

thanks help,

andy

pooyan has right idea -- long running process in background thread -- gives wrong library example, since program swing program , not android program. canonical answer swing long-running task in doinbackground() method of swingworker.

please hold while find better example...

something so:

if (buildpathset == 1) {    setcursor(cursor.getpredefinedcursor(cursor.wait_cursor));    progress.setvisible(true);     // create swingworker object    final swingworker<void, void> myworker = new swingworker<void, void>() {       protected void doinbackground() throws exception {          // here long running task, calling in background          // thread          wiredsync();          return null;       };    };     // allows me notified when swingworker has    // finished    myworker.addpropertychangelistener(new propertychangelistener() {        @override       public void propertychange(propertychangeevent pcevt) {          // if swingworker done          if (pcevt.getnewvalue() == swingworker.statevalue.done) {             // notify user             joptionpane.showmessagedialog(null, "sync complete. ",                   null, buildpathset);             setcursor(cursor.getdefaultcursor());             progress.setvisible(false);              try {                // 1 way catch errors occur in                // swingworker                myworker.get();             } catch (interruptedexception | executionexception e) {                e.printstacktrace();             }           }       }    });    // run swingworker    myworker.execute(); } else {    // warning in here later - } 

for more on this, please check out: lesson: concurrency in swing


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -