1.JFace中的进度条使用
import java.lang.reflect.InvocationTargetException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jface.dialogs.ProgressMonitorDialog; import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.swt.widgets.Shell; public class OrdinaryProgress{ private Shell shell; public OrdinaryProgress(Shell parent) { this.shell=shell; } public void run(){ try { new ProgressMonitorDialog(shell).run(true, true, new IRunnableWithProgress(){ public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { monitor.beginTask("generate", 30); for (int i = 0; i < 100; i++) { if(monitor.isCanceled()) { return ; } monitor.worked(1); Thread.sleep(50); } monitor.done(); } }); } catch (InvocationTargetException e) { } catch (InterruptedException e) { } } }
2. 使用Eclipse提供的job
代码
Job searchJob = new Job("Task") {
protected IStatus run(IProgressMonitor monitor) {
try {
monitor.beginTask("", 100);
for (int i = 0; i < 10; i++) {
Thread.sleep(500);
monitor.worked(10);
}
monitor.done();
} catch (Exception e) {
e.printStackTrace();
}
return Status.OK_STATUS;
}
};
searchJob.setUser(true); // 是否需要弹出进度窗口
searchJob.schedule();