java - JProgressBar.setMaximum(int) sometimes doesn't seems to work -
i have jprogressbar need set new minimum
, maximum
, progress
value each time task of queue done, using code below:
this.progressbar.setminimum(minimum); this.progressbar.setmaximum(maximum); this.progressbar.setvalue(progress);
but, noticed sometimes, setmaximum(int)
method doesn't seems work, because progress bar still working old maximum value.
then wrote test code , ran it:
progressbar.setmaximum(10); system.out.println(progressbar.getmaximum());
sometimes prints 10
expected, , prints old value: different 10.
i spent hours on google , javadocs, tried call revalidate()
, repaint()
on jprogressbar's parent, , nothing. tried call thread.sleep(10)
wait awt threads run tasks , didn't work.
any ideas?
edit: provided more code:
/* applet construct view , init thread. */ public class fastapplet extends applet { private jprogressbar progressbar; private jpanel panel; private runnable runnable; @override public void init() { try { java.awt.eventqueue.invokeandwait(new runnable() { @override public void run() { createandshowgui(); initthreads(); } } } catch (exception e) { e.printstacktrace(); } } private void createandshowgui() { panel = new jpanel(); progressbar = new jprogressbar(); progressbar.setstringpainted(true); panel.add(progressbar); } private void initthreads() { runnable = new myrunnable(progressbar); thread thread = new thread(runnable); thread.start(); } } /* runnable update progress , call setmaximum(int) on jprogressbar */ public class myrunnable { private int progress; private final jprogressbar progressbar; myrunnable(jprogressbar progressbar) { this.progressbar = progressbar; } @override public void run() { // tasks , update progressbar using progressbar.setvalue(progress) // when done, reset progress bar new maximum: definenewprogressbar(0, 0, newmaximum); } public void definenewprogressbar(int progress, int minimum, int maximum) { this.progress = progress; component component = progressbar.getparent(); jpanel panel = (jpanel) component; this.progressbar.setminimum(minimum); this.progressbar.setmaximum(maximum); this.progressbar.setvalue(progress); panel.revalidate(); panel.repaint(); } }
you wrote in comments call setmaximum()
not edt. should call edt this:
swingutilities.invokelater(new runnable() { @override public void run() { progressbar.setmaximum(10); } });
if use java8, can using lambda expression:
swingutilities.invokelater(()->progressbar.setmaximum(10));
Comments
Post a Comment