Swt/Jface进度条

Swt/Jface进度条

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

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) throws Exceptiondot.gif{
InBlock.gif        
final Display display = Display.getDefault();
InBlock.gif        
final Shell shell = new Shell();
InBlock.gif        shell.setSize(
500375);
InBlock.gif        shell.setText(
"SWT Application");
InBlock.gif        
InBlock.gif        
//
ExpandedSubBlockStart.gifContractedSubBlock.gif
        IRunnableWithProgress runnable = new IRunnableWithProgress()dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException dot.gif{
InBlock.gif                monitor.beginTask(
"generate"30);
ExpandedSubBlockStart.gifContractedSubBlock.gif                
for(int i=0;i<100;i++)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if(monitor.isCanceled())dot.gif{
InBlock.gif                        
return;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    monitor.worked(
1);
InBlock.gif                    Thread.sleep(
50);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
InBlock.gif                monitor.done();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
;
InBlock.gif        
new ProgressMonitorDialog(shell).run(truetrue, runnable);
InBlock.gif        shell.open();
InBlock.gif        shell.layout();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
while (!shell.isDisposed()) dot.gif{
InBlock.gif            
if (!display.readAndDispatch())
InBlock.gif                display.sleep();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
2,反复循环的进度条
None.gif import  java.lang.reflect.InvocationTargetException;
None.gif
None.gif
import  org.eclipse.core.runtime.IProgressMonitor;
None.gif
import  org.eclipse.jface.dialogs.ProgressMonitorDialog;
None.gif
import  org.eclipse.jface.operation.IRunnableWithProgress;
None.gif
import  org.eclipse.swt.widgets.Display;
None.gif
import  org.eclipse.swt.widgets.Shell;
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  TestProgress  dot.gif {
InBlock.gif    
static boolean stopflg = false;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * Launch the application
InBlock.gif     * 
@param args
ExpandedSubBlockEnd.gif     
*/
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) throws Exceptiondot.gif{
InBlock.gif        
final Display display = Display.getDefault();
InBlock.gif        
final Shell shell = new Shell();
InBlock.gif        shell.setSize(
500375);
InBlock.gif        shell.setText(
"SWT Application");
InBlock.gif        
InBlock.gif        
//
ExpandedSubBlockStart.gifContractedSubBlock.gif
        IRunnableWithProgress runnable = new IRunnableWithProgress()dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException dot.gif{
InBlock.gif                monitor.beginTask(
"generate"30);
InBlock.gif                
int i=0;
ExpandedSubBlockStart.gifContractedSubBlock.gif                
while(true)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if(stopflg)dot.gif{
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    i
++;
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if(i==30)dot.gif{
InBlock.gif                        i
=0;
InBlock.gif                        monitor.beginTask(
"generate"30);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    monitor.worked(
1);
InBlock.gif                    Thread.sleep(
100);
ExpandedSubBlockEnd.gif                }

InBlock.gif                monitor.done();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
;
InBlock.gif        
new ProgressMonitorDialog(shell).run(truetrue, runnable);
InBlock.gif        shell.open();
InBlock.gif        shell.layout();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
while (!shell.isDisposed()) dot.gif{
InBlock.gif            
if (!display.readAndDispatch())
InBlock.gif                display.sleep();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

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

转载于:https://www.cnblogs.com/kentyshang/archive/2007/08/16/858303.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值