JFace实现进度条

//这个进度条是别人写的,呵呵

 

import java.awt.Point;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

public class ORProgressBar {
 /**
  * Logger for this class
  */

 public boolean  isDispose = false;
 private Shell  shell  = null;
 private Display  display  = null;
 private String  message  = "";
 private String  title  = "";
 private ProgressBar pbar  = null;
 private Object  obj   = null;

 /**
  * 通用Eclipse平台进度条
  * @param pObj 调用类实例
  * @param ptitle 进度条标题
  * @param pmsg 进度条显示信息
  */
 public ORProgressBar(Object pObj, String ptitle, String pmsg) {
  obj = pObj;
  message = pmsg;
  title = ptitle;
  init();
 }

 private void init() {
  try {
   display = new Display();
  } catch (Exception e) {
   try {
    display = Display.getDefault();
   } catch (Exception e1) {
    display = Display.getCurrent();
   }
  }
  shell = new Shell(display);
  shell.setSize(335, 100);
  Point p = UtilApi.getMsgboxLocate();
  shell.setLocation(p.x, p.y);
  pbar = new ProgressBar(shell, SWT.INDETERMINATE);
  pbar.setBounds(10, 30, 300, 20);
  shell.setText(title);
  Label label = new Label(shell, SWT.NULL);
  label.setText(this.message);
  label.setAlignment(SWT.CENTER);
  label.setBounds(10, 10, 300, 20);
  shell.setEnabled(false);
 }

 /**
  * doProcess:调用实例类progress方法
  * @param @return 设定文件
  * @return boolean 对象类型
  * @throws
  * @since CodingExample Ver 1.1
  */
 private boolean doProcess() {
  try {
   Class cls = obj.getClass();
   //通过反射机制调用传入类中声明的方法。
   Method mth = cls.getDeclaredMethod("progress", null);
   mth.invoke(obj, null);
  } catch (SecurityException e) {
   e.printStackTrace();
  } catch (IllegalArgumentException e) {
   e.printStackTrace();
  } catch (NoSuchMethodException e) {

   e.printStackTrace();
  } catch (IllegalAccessException e) {
   e.printStackTrace();
  } catch (InvocationTargetException e) {
   e.printStackTrace();
  }
  return true;
 }

 public void run() {
  shell.open();
  if (display == null)
   display = Display.getDefault();
  if (display != null && !display.isDisposed()) {
   Thread t1 = new Thread() {
    public void run() {
     isDispose = doProcess();
    }
   };
   t1.start();
   System.out.println("111");
   display.syncExec(new Runnable() {
    public void run() {
     while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
       if (!isDispose) {
        try {
         shell.forceActive();
         display.getThread().sleep(100);
        } catch (InterruptedException e) {
         e.printStackTrace();
        }
       } else {
        display.dispose();
       }
      }
     }
    }
   });
  }
 }

 public static void main(String[] args) {
  ORProgressBar t = new ORProgressBar(null, "", "");
  ORProgressBar bar = new ORProgressBar(t, "进度", "正在...,请稍候...");
  bar.run();
 }

 /**
  * progress:实例对象中运行内容(必须存在)
  * @param 设定文件
  * @return void 对象类型
  * @throws
  * @since CodingExample Ver 1.1
  */
 public void progress() {
  System.out.println("Progress()----------");
  try {
   Thread.sleep(4000);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
 }

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值