更新 JProgressBar
我无法更新我的进度条...这是我的代码
Thread t=new Thread(new Runnable(){
public void run(){
int i=1;
jProgBar.setMinimum(0);
jProgBar.setMaximum(100);
try {
while(i<=100 || true){
jProgBar.setValue(i);
i++;
Thread.sleep(50);
}
}
catch (InterruptedException ex){
jProgBar.setValue(jProgBar.getMaximum());
}
}
});
t.start();
.... Something code that correctly works
t.interrupt();
进度条状态仅在线程结束时更新。 有人可以帮助我吗?
I can't update my progressbar... this is my code
Thread t=new Thread(new Runnable(){
public void run(){
int i=1;
jProgBar.setMinimum(0);
jProgBar.setMaximum(100);
try {
while(i<=100 || true){
jProgBar.setValue(i);
i++;
Thread.sleep(50);
}
}
catch (InterruptedException ex){
jProgBar.setValue(jProgBar.getMaximum());
}
}
});
t.start();
.... Something code that correctly works
t.interrupt();
The progress bar state is updated only at the end of thread.
Can someone help me??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在睡眠之前,添加对 SwingUtilties.invokeLater() 的调用,该调用会生成一个线程以在 EDT 中的进度条上触发 firePropertyChange。
Before the sleep, add a call to SwingUtilties.invokeLater() that spawns a thread to fire a firePropertyChange on the progressbar in the EDT.
直接使用模型而不是 JProgressBar:
以下示例运行良好:
Use a model instead of the JProgressBar directly:
The following example works fine:
针对您的情况的最佳建议是使用 SwingWorker。查看 API http://java.sun .com/javase/6/docs/api/javax/swing/SwingWorker.html
重写流程方法以更新进度条的值(然后它将在 EDT 上正确完成)
更多信息可以在 http://java.sun.com/products/jfc/tsc/articles /threads/threads2.html
The best advice for your situation is to use SwingWorker. Check out the API at http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html
Override process method to update value of progress bar ( then it will be done correctly on EDT)
More info can be obtained at http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html
根据 eugener 的说法,SwingWorker 绝对是您想要在此处使用的工具,或者任何时候生成长时间运行的任务时可能会在完成之前锁定 GUI 的工具。 Sun^H^H^HOracle 提供了有关在
SwingWorker
中使用进度条的完整教程:http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html
Per eugener,
SwingWorker
is definitely what you want to be using here, or any time a long-running task is spawned that could otherwise lock up your GUI prior to completion. A full tutorial on using progress bars withSwingWorker
is available from Sun^H^H^HOracle here:http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html
谢谢大家。
我是这样解决的
Thanks All.
I solved in this way
将此代码片段放在
“i++”和“Thread.sleep()”之间应该可以完成这项工作。要使其编译,请将 jProgBar 标记为“最终”。
Putting this snippet
Between 'i++' and 'Thread.sleep()' should do the job. To get it compiling, mark jProgBar as 'final'.