[英]JProgressBar.setMaximum(int) sometimes doesn't seems to work
I have a JProgressBar that I need to set a new minimum
, maximum
and progress
value each time a task of a queue is done, using the code below: 我有一个JProgressBar,每次完成队列任务时,我需要使用以下代码设置新的
minimum
, maximum
和progress
值:
this.progressBar.setMinimum(minimum);
this.progressBar.setMaximum(maximum);
this.progressBar.setValue(progress);
But, I noticed that sometimes, the setMaximum(int)
method doesn't seems to work, because the progress bar still working with the old maximum value. 但是,我注意到有时
setMaximum(int)
方法似乎不起作用,因为进度条仍然可以使用旧的最大值工作。
Then I wrote some test code and ran it: 然后我编写了一些测试代码并运行了它:
progressBar.setMaximum(10);
System.out.println(progressBar.getMaximum());
Sometimes it prints 10
as expected, and sometimes prints the old value: anything different from 10. 有时它会按预期打印
10
,有时会打印旧值:任何不同于10的值。
I spent hours on Google and javadocs, tried to call revalidate()
and repaint()
on JProgressBar's parent, and nothing. 我在Google和javadocs上花费了数小时,试图在JProgressBar的父级上调用
revalidate()
和repaint()
,却一无所获。 I also tried to call Thread.sleep(10)
to wait AWT threads to run another tasks and didn't work. 我还尝试调用
Thread.sleep(10)
来等待AWT线程运行其他任务,但无法正常工作。
Any ideas? 有任何想法吗?
EDIT: Provided some more code: 编辑:提供了更多的代码:
/* Applet that construct the view and init a 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 that update progress and 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() {
// do the tasks and update the progressBar using progressBar.setValue(progress)
// when done, reset the progress bar with the a 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 your comments that you call setMaximum()
not from the EDT. 您在注释中写道,不是从EDT调用
setMaximum()
。 You should call it from the EDT like this: 您应该像这样从EDT调用它:
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
progressBar.setMaximum(10);
}
});
If you use Java8, you can do this with using a lambda expression: 如果使用Java8,则可以使用lambda表达式来实现:
SwingUtilities.invokeLater(()->progressBar.setMaximum(10));
You can use SafeProgressBarUpdaterThread : 您可以使用SafeProgressBarUpdaterThread :
progressBarUpdater = new SafeProgressBarUpdaterThread(progressBar);
//...
progressBarUpdater.setMaximum(maximum);
progressBarUpdater.setValue(value);
java.awt.EventQueue.invokeLater(progressBarUpdater);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.