处理长时间的任务的时候常需要进度条显示,有几种实现方式
1,普通的进度条
importorg.eclipse.core.runtime.IProgressMonitor;
importorg.eclipse.jface.dialogs.ProgressMonitorDialog;
importorg.eclipse.jface.operation.IRunnableWithProgress;
importorg.eclipse.swt.widgets.Display;
importorg.eclipse.swt.widgets.Shell;
publicclassTestProgress{
staticbooleanstopflg=false;
/***//**
*Launchtheapplication
*@paramargs
*/
publicstaticvoidmain(String[]args)throwsException{
finalDisplaydisplay=Display.getDefault();
finalShellshell=newShell();
shell.setSize(500,375);
shell.setText("SWTApplication");
//
IRunnableWithProgressrunnable=newIRunnableWithProgress(){
publicvoidrun(IProgressMonitormonitor)throwsInvocationTargetException,InterruptedException{
monitor.beginTask("generate",30);
for(inti=0;i<100;i++){
if(monitor.isCanceled()){
return;
}
monitor.worked(1);
Thread.sleep(50);
}
monitor.done();
}
};
newProgressMonitorDialog(shell).run(true,true,runnable);
shell.open();
shell.layout();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
}
}
2,反复循环的进度条
importorg.eclipse.core.runtime.IProgressMonitor;
importorg.eclipse.jface.dialogs.ProgressMonitorDialog;
importorg.eclipse.jface.operation.IRunnableWithProgress;
importorg.eclipse.swt.widgets.Display;
importorg.eclipse.swt.widgets.Shell;
publicclassTestProgress{
staticbooleanstopflg=false;
/***//**
*Launchtheapplication
*@paramargs
*/
publicstaticvoidmain(String[]args)throwsException{
finalDisplaydisplay=Display.getDefault();
finalShellshell=newShell();
shell.setSize(500,375);
shell.setText("SWTApplication");
//
IRunnableWithProgressrunnable=newIRunnableWithProgress(){
publicvoidrun(IProgressMonitormonitor)throwsInvocationTargetException,InterruptedException{
monitor.beginTask("generate",30);
inti=0;
while(true){
if(stopflg){
break;
}
i++;
if(i==30){
i=0;
monitor.beginTask("generate",30);
}
monitor.worked(1);
Thread.sleep(100);
}
monitor.done();
}
};
newProgressMonitorDialog(shell).run(true,true,runnable);
shell.open();
shell.layout();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
}
}
3,rcp中后台任务的进度条
使用Job建立后台任务,只需要设置job.setUser(true)进度条就出现了,和上边一样,进度条需要自己来控制进度。如果做一个cool的进度条,就看你如何让进度条显示出实际的任务进程。