java - Problem making a JProgressBar update values in Loop (Threaded) -
am trying program update progress bar values within method while performing operations. not happen until end, , ui freezes.
after looking around similar questions problems, tried implement accepted solutions (using threads) cannot work correctly. if not there.
my program contains several classes, main
being 1 automatically created netbeans on jframe design mode, there things such static void main
, public main
not sure of of contents. under put snippets of methods, thread implementation.
public class main extends javax.swing.jframe implements actionlistener, runnable{ // added implements actlis, runn..... ... static main _this; // included variable ... public static void main(string args[]) { main m = new main(); // added me new thread(m).start(); // added me java.awt.eventqueue.invokelater(new runnable() { public void run() { new main().setvisible(true); } }); } ... public main() { initcomponents(); _this = this; // added me } ... // included these 2 methods in class public void actionperformed(actionevent e) { synchronized(this){ notifyall(); } } public void run() { try{synchronized(this){wait();}} catch (interruptedexception e){} progressbar.setvalue(50); } ... private void buttonpressed(java.awt.event.mouseevent evt) { for(int i=0; i<=100; i++) { for(int j=0; j<=5; j++) { // work } run(); } }
all things commented i added...
things putted according tutorials , answers have seen online, nothing seems work , feels have tried close million different combinations...
thanks in advance helping out.
here's basis at, if can study , understand why every piece of code there think help. feel free ask questions in comment (although i'm going bed right now!)
example:
public class progressbardemo extends jframe { private final jprogressbar progressbar = new jprogressbar(0, 100); private int progresscounter = 0; public progressbardemo() { setcontentpane(progressbar); setpreferredsize(new dimension(100, 100)); setdefaultcloseoperation(exit_on_close); pack(); new thread(new runnable() { public void run() { while (progresscounter <= 100) { swingutilities.invokelater(new runnable() { public void run() { progressbar.setvalue(progresscounter++); } }); try { thread.sleep(500); } catch (interruptedexception e) {} } } }).start(); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { new progressbardemo().setvisible(true); } }); } }
two different ways approach problem, using swingworker instead:
swingworker example 1:
.... public progressbardemo() { setcontentpane(progressbar); setpreferredsize(new dimension(100, 100)); setdefaultcloseoperation(exit_on_close); pack(); swingworker<integer, void> worker = new swingworker<integer,void>() { public integer doinbackground() { while (progresscounter <= 100) { setprogress(progresscounter++); try { thread.sleep(500); } catch (interruptedexception e) {} } return 0; } }; worker.addpropertychangelistener(new propertychangelistener() { public void propertychange(propertychangeevent event) { if ("progress".equals(event.getpropertyname())) { progressbar.setvalue((integer)event.getnewvalue()); } } }); worker.execute(); } ....
swingworker example 2 (not nice, interesting nonetheless):
.... public progressbardemo() { setcontentpane(progressbar); setpreferredsize(new dimension(100, 100)); setdefaultcloseoperation(exit_on_close); pack(); new swingworker<integer,integer>() { public integer doinbackground() { while (progresscounter <= 100) { publish(progresscounter++); try { thread.sleep(500); } catch (interruptedexception e) {} } return 0; } public void process(list<integer> progresses) { integer maxprogress = null; (int progress : progresses) { if (maxprogress == null || progress > maxprogress) { maxprogress = progress; } } progressbar.setvalue(maxprogress); } }.execute(); } ....
Comments
Post a Comment