进度条对话框 : 进度条 « SWT-JFace-Eclipse « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » SWT-JFace-Eclipse » 进度条屏幕截图 
进度条对话框
进度条对话框



import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

/**
 * progress bar dialog.
 * the first, you must know your app execute times,
 * you need implement two method: 
 
 * process(int times);
 * initGuage();
 
 * you can implements method:
 *  
 * cleanUp()
 * doBefore()
 * doAfter() 
 @author yin_zhiguo
 *         yin_zhiguo@hotmail.com
 
 */
public abstract class ProgressBarDialog extends Dialog {

    private Label processMessageLabel; //info of process finish 
    private Button cancelButton; //cancel button
    private Composite cancelComposite;
    private Label lineLabel;//
    private Composite progressBarComposite;//
    private CLabel message;//
    private ProgressBar progressBar = null//

    private Object result; //
    private Shell shell; //
    private Display display = null
    
    protected volatile boolean isClosed = false;//closed state
    
    protected int executeTime = 50;//process times
    protected String processMessage = "process......";//procress info
    protected String shellTitle = "Progress..."//
    protected Image processImage = SWTUtil.getImageOfMessage();//image
    protected boolean mayCancel = true//cancel
    protected int processBarStyle = SWT.SMOOTH; //process bar style

  public void setMayCancel(boolean mayCancel) {
    this.mayCancel = mayCancel;
  }

    public void setExecuteTime(int executeTime) {
        this.executeTime = executeTime;
    }

    public void setProcessImage(Image processImage) {
    this.processImage = processImage;
  }

  public void setProcessMessage(String processInfo) {
    this.processMessage = processInfo;
  }

  public ProgressBarDialog(Shell parent) {
        super(parent);
    }
    
  
    public abstract void initGuage();

    public Object open() {
        createContents()//create window
        shell.open();
        shell.layout();
        
        //start work
        new ProcessThread(executeTime).start();

        Display display = getParent().getDisplay();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        return result;
    }

    protected void createContents() {
        shell = new Shell(getParent(), SWT.TITLE | SWT.PRIMARY_MODAL);
        display = shell.getDisplay();
        final GridLayout gridLayout = new GridLayout();
        gridLayout.verticalSpacing = 10;

        shell.setLayout(gridLayout);
        shell.setSize(483181);
        shell.setText(shellTitle);

        final Composite composite = new Composite(shell, SWT.NONE);
        composite.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
        composite.setLayout(new GridLayout());

        message = new CLabel(composite, SWT.NONE);
        message.setImage(processImage);
        message.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
        message.setText(processMessage);

        progressBarComposite = new Composite(shell, SWT.NONE);
        progressBarComposite.setLayoutData(new GridData(GridData.FILL,
                GridData.CENTER, false, false));
        progressBarComposite.setLayout(new FillLayout());

        progressBar = new ProgressBar(progressBarComposite, processBarStyle);
        progressBar.setMaximum(executeTime);

        processMessageLabel = new Label(shell, SWT.NONE);
        processMessageLabel.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, false, false));
        lineLabel = new Label(shell, SWT.HORIZONTAL | SWT.SEPARATOR);
        lineLabel.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, false, false));

        cancelComposite = new Composite(shell, SWT.NONE);
        cancelComposite.setLayoutData(new GridData(GridData.END,
                GridData.CENTER, false, false));
        final GridLayout gridLayout_1 = new GridLayout();
        gridLayout_1.numColumns = 2;
        cancelComposite.setLayout(gridLayout_1);

        cancelButton = new Button(cancelComposite, SWT.NONE);
        cancelButton.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                isClosed = true;
                //System.out.println(isClosed);
            }
        });
        cancelButton.setLayoutData(new GridData(78, SWT.DEFAULT));
        cancelButton.setText("cancel");
        cancelButton.setEnabled(this.mayCancel);

    }

    protected abstract String process(int times);


    protected void cleanUp()
    {
      
    }
    

    protected void doBefore()
    {
      
    }
    
    protected void doAfter()
    {
      
    }

    class ProcessThread extends Thread {
        private int max = 0;
        private volatile boolean shouldStop = false;

        ProcessThread(int max) {
            this.max = max;
        }

        public void run() {
          doBefore();
            for (final int[] i = new int[] {1}; i[0<= max; i[0]++
            {
                //
                final String info = process(i[0]);
                if (display.isDisposed()) {
                    return;
                }
                display.syncExec(new Runnable() {
                    public void run() {
                        if (progressBar.isDisposed()) {
                            return;
                        }
                        //
                        processMessageLabel.setText(info);
                        //
                        progressBar.setSelection(i[0]);
                        //
                        if (i[0== max || isClosed) {
                            if (isClosed) {
                                shouldStop = true;//
                                cleanUp();//
                            }
                            shell.close();//
                        }
                    }
                });

                if (shouldStop) {
                    break;
                }
            }
            doAfter();
        }
    }

  public void setShellTitle(String shellTitle) {
    this.shellTitle = shellTitle;
  }

  public void setProcessBarStyle(boolean pStyle) {
    if(pStyle)
      this.processBarStyle = SWT.SMOOTH;
    else
      this.processBarStyle = SWT.NONE;
      
  }
}


class DemoProgressBar extends ProgressBarDialog {
  
  private String[] info = null;
  
  public DemoProgressBar(Shell parent) {
       super(parent);
   }
  

  @Override
  public void initGuage() {
    info = new String[100];
    for (int i = 0; i < info.length; i++) {
      info[i"process task " + i + ".";
    }
    this.setExecuteTime(100);
    this.setMayCancel(true);
    this.setProcessMessage("please waiting....");
    this.setShellTitle("Demo");

  }

  @Override
  protected String process(int arg0) {
    try{
      Thread.sleep((long)(Math.random() 300));
    }catch(Exception e){e.printStackTrace();}
    
    return info[arg0 - 1];
  }

}


class SWTUtil {


    private static ImageRegistry imageRegistry = new ImageRegistry();

    private static Clipboard clipboard = new Clipboard(Display.getCurrent());
    
    private static final String ___IMAGE_Of_MESSAGE = "";
    
    static{
      imageRegistry.put(___IMAGE_Of_MESSAGE, ImageDescriptor.createFromURL(newURL("file:icons/info2.gif")));
    }


    private SWTUtil(){}

    
    public static URL newURL(String url_name) {
        try {
            return new URL(url_name);
        catch (MalformedURLException e) {
            throw new RuntimeException("Malformed URL " + url_name, e);
        }
    }
    
    
    public static void registryImage(String id, String urlName)
    {
      imageRegistry.put(id, ImageDescriptor.createFromURL(newURL(urlName)));
    }
    
    
    public static Image getImage(String id)
    {
      return imageRegistry.get(id);
    }

    
    public static Clipboard getClipboard() {
        return clipboard;
    }
    
    
    public static Image getImageOfMessage()
    {
      return imageRegistry.get(___IMAGE_Of_MESSAGE);
    }
}

class PBDialogDemo extends Shell {

  public static void main(String args[]) {
    try {
      Display display = Display.getDefault();
      PBDialogDemo shell = new PBDialogDemo(display, SWT.SHELL_TRIM);
      shell.open();
      shell.layout();
      while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
          display.sleep();
      }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  public PBDialogDemo(Display display, int style) {
    super(display, style);
    createContents();
  }

  protected void createContents() {
    setText("ProgressBar Dialog");
    setSize(21898);
    setLayout(new FillLayout());

    final Button openProgressbarDialogButton = new Button(this, SWT.NONE);
    openProgressbarDialogButton.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        DemoProgressBar dpb = new DemoProgressBar(PBDialogDemo.this);
        dpb.initGuage();
        dpb.open();
      }
    });
    openProgressbarDialogButton.setText("Open ProgressBar Dialog");
    
  }

  protected void checkSubclass() {
  }

}

           
       
ProgressBarDialog_src.zip( 20 k)
Related examples in the same category
1. 更新SWT进度栏(从另一个线程)更新SWT进度栏(从另一个线程)
2. 更新进度栏(从用户界面线程)更新进度栏(从用户界面线程)
3. 计数计数
4. 进度条实例进度条实例
5. 显示进度条显示进度条
6. 进度实例进度实例
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.