Swt/Jface进度条

Swt/Jface进度条

处理长时间的任务的时候常需要进度条显示,有几种实现方式
1,普通的进度条
Swt/Jface进度条 import  java.lang.reflect.InvocationTargetException;
Swt/Jface进度条
Swt/Jface进度条
import  org.eclipse.core.runtime.IProgressMonitor;
Swt/Jface进度条
import  org.eclipse.jface.dialogs.ProgressMonitorDialog;
Swt/Jface进度条
import  org.eclipse.jface.operation.IRunnableWithProgress;
Swt/Jface进度条
import  org.eclipse.swt.widgets.Display;
Swt/Jface进度条
import  org.eclipse.swt.widgets.Shell;
Swt/Jface进度条
Swt/Jface进度条
Swt/Jface进度条
public   class  TestProgress  {
Swt/Jface进度条    
static boolean stopflg = false;
Swt/Jface进度条    
/**
Swt/Jface进度条     * Launch the application
Swt/Jface进度条     * 
@param args
Swt/Jface进度条     
*/

Swt/Jface进度条    
public static void main(String[] args) throws Exception{
Swt/Jface进度条        
final Display display = Display.getDefault();
Swt/Jface进度条        
final Shell shell = new Shell();
Swt/Jface进度条        shell.setSize(
500375);
Swt/Jface进度条        shell.setText(
"SWT Application");
Swt/Jface进度条        
Swt/Jface进度条        
//
Swt/Jface进度条
        IRunnableWithProgress runnable = new IRunnableWithProgress(){
Swt/Jface进度条            
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
Swt/Jface进度条                monitor.beginTask(
"generate"30);
Swt/Jface进度条                
for(int i=0;i<100;i++){
Swt/Jface进度条                    
if(monitor.isCanceled()){
Swt/Jface进度条                        
return;
Swt/Jface进度条                    }

Swt/Jface进度条                    monitor.worked(
1);
Swt/Jface进度条                    Thread.sleep(
50);
Swt/Jface进度条                }

Swt/Jface进度条                
Swt/Jface进度条                monitor.done();
Swt/Jface进度条            }

Swt/Jface进度条        }
;
Swt/Jface进度条        
new ProgressMonitorDialog(shell).run(truetrue, runnable);
Swt/Jface进度条        shell.open();
Swt/Jface进度条        shell.layout();
Swt/Jface进度条        
while (!shell.isDisposed()) {
Swt/Jface进度条            
if (!display.readAndDispatch())
Swt/Jface进度条                display.sleep();
Swt/Jface进度条        }

Swt/Jface进度条    }

Swt/Jface进度条
Swt/Jface进度条}

Swt/Jface进度条
2,反复循环的进度条
Swt/Jface进度条 import  java.lang.reflect.InvocationTargetException;
Swt/Jface进度条
Swt/Jface进度条
import  org.eclipse.core.runtime.IProgressMonitor;
Swt/Jface进度条
import  org.eclipse.jface.dialogs.ProgressMonitorDialog;
Swt/Jface进度条
import  org.eclipse.jface.operation.IRunnableWithProgress;
Swt/Jface进度条
import  org.eclipse.swt.widgets.Display;
Swt/Jface进度条
import  org.eclipse.swt.widgets.Shell;
Swt/Jface进度条
Swt/Jface进度条
Swt/Jface进度条
public   class  TestProgress  {
Swt/Jface进度条    
static boolean stopflg = false;
Swt/Jface进度条    
/**
Swt/Jface进度条     * Launch the application
Swt/Jface进度条     * 
@param args
Swt/Jface进度条     
*/

Swt/Jface进度条    
public static void main(String[] args) throws Exception{
Swt/Jface进度条        
final Display display = Display.getDefault();
Swt/Jface进度条        
final Shell shell = new Shell();
Swt/Jface进度条        shell.setSize(
500375);
Swt/Jface进度条        shell.setText(
"SWT Application");
Swt/Jface进度条        
Swt/Jface进度条        
//
Swt/Jface进度条
        IRunnableWithProgress runnable = new IRunnableWithProgress(){
Swt/Jface进度条            
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
Swt/Jface进度条                monitor.beginTask(
"generate"30);
Swt/Jface进度条                
int i=0;
Swt/Jface进度条                
while(true){
Swt/Jface进度条                    
if(stopflg){
Swt/Jface进度条                        
break;
Swt/Jface进度条                    }

Swt/Jface进度条                    i
++;
Swt/Jface进度条                    
if(i==30){
Swt/Jface进度条                        i
=0;
Swt/Jface进度条                        monitor.beginTask(
"generate"30);
Swt/Jface进度条                    }

Swt/Jface进度条                    monitor.worked(
1);
Swt/Jface进度条                    Thread.sleep(
100);
Swt/Jface进度条                }

Swt/Jface进度条                monitor.done();
Swt/Jface进度条            }

Swt/Jface进度条        }
;
Swt/Jface进度条        
new ProgressMonitorDialog(shell).run(truetrue, runnable);
Swt/Jface进度条        shell.open();
Swt/Jface进度条        shell.layout();
Swt/Jface进度条        
while (!shell.isDisposed()) {
Swt/Jface进度条            
if (!display.readAndDispatch())
Swt/Jface进度条                display.sleep();
Swt/Jface进度条        }

Swt/Jface进度条    }

Swt/Jface进度条
Swt/Jface进度条}

Swt/Jface进度条
3,rcp中后台任务的进度条
使用Job建立后台任务,只需要设置job.setUser(true)进度条就出现了,和上边一样,进度条需要自己来控制进度。如果做一个cool的进度条,就看你如何让进度条显示出实际的任务进程。

你可能感兴趣的:(jface)