swing - Progress bars in Java -
i have code 2 methods.
public void fondo() { ... } //gathers jframe background , system time public void recuperardatosinternet() {...} //connects url , gets data.
when jframe running, @ beginning takes 4 or 5 seconds perform operations of methods.
while it's loading, frame displays totally empty 3 or 4 seconds until methods complete, frame shows , it's right.
how can make progress bar shows user it's loading? don't mean progressbar predetermined take "4000 ms". referring progressbar can take whatever takes, , bar doesn't reach 100% until methods complete.
if run heavy task in the event dispatch thread it's gonna freeze until finish avoid can execute download in thread using swingworker
. follow link see complete example progressbar
, special attention setprogress() publish() , process().
example:
public class myworker extends swingworker<integer, string> { @override protected integer doinbackground() throws exception { // start publish("start download"); setprogress(1); // more work done publish("more work done"); setprogress(10); // complete publish("complete"); setprogress(100); return 1; } @override protected void process(list< string> chunks) { // messages received doinbackground() (when invoking publish() method) } }
and in client code:
swingworker worker = new myworker(); worker.addpropertychangelistener(new myprogresslistener()); worker.execute(); class myprogresslistener implements propertychangelistener { @override public void propertychange(final propertychangeevent event) { if(event.getpropertyname().equalsignorecase("progress")) { downloadprogressbar.setindeterminate(false); downloadprogressbar.setvalue((integer) event.getnewvalue()); } } }
Comments
Post a Comment