swing - Setting up a progress bar in Java -
i have program , have tried implement progress bar code. bellow example of code currently. main gui in own class , instantiates other classes execute code within classes' methods. example follows:
class mainclass { public javax.swing.jprogressbar progressbar; private void combineactionperformed(java.awt.event.actionevent evt) { combine combiner = new combine(); combiner.merge(folder); } }
it takes folder listing , goes combine class has following code:
public class combine extends swingworker<integer,integer>{ public void merge(folder []){ (for int i=0;i<folder.length;i++){ merge(folder[i]); } public void merge(folder[]){ output stream; } }
how implement swing worker in example make progress update mainclass progress bar each iteration of occurs?
to begin, worker missing methods should implement, such doinbackground()
, done()
. need constructor pass folder[]
.
public class combine extends swingworker<integer,integer>{ folder[] folders; public combine (folder[] folders) { this.folders = folders; } private void merge(folder []) { (for int i=0;i<folder.length;i++) { merge(folder[i]); //send message of progress here, executed //from doinbackground() setprogress(....); } } private void merge(folder){ output stream; } protected integer doinbackground() { merge(folders); return null; } protected void done() { .... } }
then call worker with
combine combiner = new combine(folders); combiner.execute();
to track progress, example swingworker
api:
combiner.addpropertychangelistener( new propertychangelistener() { public void propertychange(propertychangeevent evt) { if ("progress".equals(evt.getpropertyname())) { progressbar.setvalue((integer)evt.getnewvalue()); } } });
Comments
Post a Comment