[英]Updating the progress bar
I have a big program which needs to be called by a GUI. 我有一个大程序,需要通过GUI调用。 The GUI has a progress bar which needs to be updated(like 5% .... 10% )after the user presses the start button.
GUI上有一个进度条,需要在用户按下开始按钮后进行更新(例如5%.... 10%)。 The problem is that the background task performed does not have a fixed execution time.
问题是执行的后台任务没有固定的执行时间。 So is somehow possible to measure the progress of the task performed in the doInBackground() method (i am trying to use SwingWorker).
因此,可以某种方式测量在doInBackground()方法中执行的任务的进度(我正在尝试使用SwingWorker)。 Or should i go with an indeterminate progress bar.
还是我应该使用不确定的进度栏。 I was unable to clearly understand the example provided on Oracle's tutorial page and wasn't able to find a decent page explaining how to use a progress bar.
我无法清楚地理解Oracle教程页面上提供的示例,也无法找到说明如何使用进度条的体面的页面。 Any help will be highly appreciated.
任何帮助将不胜感激。
According to the problem, I would use a infinite progress bar 根据问题,我将使用无限进度条
public class Indeterminate extends JProgressBar {
private static final long serialVersionUID = -281761375041347893L;
/***
* initial the ProgressBar
*/
public IndeterminateProgressBar() {
super();
setIndeterminate(true);
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
setVisible(false);
}
});
}
/**
* call this, if you start a long action
*/
public void start() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
setVisible(true);
}
});
}
/**
* if you have finished, call this
*/
public void end() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
setVisible(false);
}
});
}
}
Used like this: 像这样使用:
ActionListener startButtonListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Thread(new Runnable() {
@Override
public void run() {
try {
progressBar.start();
// long operation
} catch (Exception e) {
// handle excpetion
} finally {
progressBar.end();
}
}
}).start();
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.