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.gif
public   class  TestProgress  {
InBlock.gif    
static boolean stopflg = false;
ExpandedSubBlockStart.gif    
/**
InBlock.gif     * Launch the application
InBlock.gif     * 
@param args
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    
public static void main(String[] args) throws Exception{
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.gif
        IRunnableWithProgress runnable = new IRunnableWithProgress(){
ExpandedSubBlockStart.gif            
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
InBlock.gif                monitor.beginTask(
"generate"30);
ExpandedSubBlockStart.gif                
for(int i=0;i<100;i++){
ExpandedSubBlockStart.gif                    
if(monitor.isCanceled()){
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.gif        
while (!shell.isDisposed()) {
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.gif
public   class  TestProgress  {
InBlock.gif    
static boolean stopflg = false;
ExpandedSubBlockStart.gif    
/**
InBlock.gif     * Launch the application
InBlock.gif     * 
@param args
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    
public static void main(String[] args) throws Exception{
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.gif
        IRunnableWithProgress runnable = new IRunnableWithProgress(){
ExpandedSubBlockStart.gif            
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
InBlock.gif                monitor.beginTask(
"generate"30);
InBlock.gif                
int i=0;
ExpandedSubBlockStart.gif                
while(true){
ExpandedSubBlockStart.gif                    
if(stopflg){
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    i
++;
ExpandedSubBlockStart.gif                    
if(i==30){
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.gif        
while (!shell.isDisposed()) {
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的进度条,就看你如何让进度条显示出实际的任务进程。

本文转自kenty博客园博客,原文链接http://www.cnblogs.com/kentyshang/archive/2007/08/16/858303.html如需转载请自行联系原作者


kenty

相关文章
|
6天前
swing/swt 支持多屏幕显示
swing/swt 支持多屏幕显示
|
9月前
|
Shell API
【SWT】常用代码(三)
【SWT】常用代码(三)
97 0
|
9月前
|
Java
【SWT】常用代码(二)
【SWT】常用代码(二)
104 0
|
Shell
SWT的PaintListener
以前很少用到这个类(org.eclipse.swt.events.PaintListener),利用它可以用来在control上画一些东西,基本方法是在control上 addPaintListener()一个PaintListener,然后在这个listener里做具体的画图工作,listener在control需要绘制的时候调用。
1285 0
|
Android开发 Java 关系型数据库
安装JavaFX Scene Builder 到Eclipse
JavaFX Scene Builder是一种可视布局工具,允许用户快速设计JavaFX应用程序用户界面,而无需编码。用户可以将UI组件拖放到工作区,修改其属性,应用样式表,并且它们正在创建的布局的FXML代码将在后台自动生成。
2237 0
|
前端开发 Android开发
在SWT里显示AWT对象
今天遇到一个问题,就是要在一个Eclipse插件里显示JFreeChart的图形,因为后者是基于Java2D的,要把图形显示在SWT应用程序里需要利用SWT-AWT桥接器来实现,虽说桥接的方式多半会伴随着性能下降,但总归是一个解决方法。
1298 0
|
Shell Windows Android开发