隨筆 - 4  文章 - 10  trackbacks - 0
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(1)

          隨筆檔案

          文章分類

          文章檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          處理長時間的任務的時候常需要進度條顯示,有幾種實現方式
          1,普通的進度條
          import java.lang.reflect.InvocationTargetException;

          import org.eclipse.core.runtime.IProgressMonitor;
          import org.eclipse.jface.dialogs.ProgressMonitorDialog;
          import org.eclipse.jface.operation.IRunnableWithProgress;
          import org.eclipse.swt.widgets.Display;
          import org.eclipse.swt.widgets.Shell;


          public class TestProgress {
              
          static boolean stopflg = false;
              
          /**
               * Launch the application
               * 
          @param args
               
          */

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

                              monitor.worked(
          1);
                              Thread.sleep(
          50);
                          }

                          
                          monitor.done();
                      }

                  }
          ;
                  
          new ProgressMonitorDialog(shell).run(truetrue, runnable);
                  shell.open();
                  shell.layout();
                  
          while (!shell.isDisposed()) {
                      
          if (!display.readAndDispatch())
                          display.sleep();
                  }

              }


          }

          2,反復循環的進度條
          import java.lang.reflect.InvocationTargetException;

          import org.eclipse.core.runtime.IProgressMonitor;
          import org.eclipse.jface.dialogs.ProgressMonitorDialog;
          import org.eclipse.jface.operation.IRunnableWithProgress;
          import org.eclipse.swt.widgets.Display;
          import org.eclipse.swt.widgets.Shell;


          public class TestProgress {
              
          static boolean stopflg = false;
              
          /**
               * Launch the application
               * 
          @param args
               
          */

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

                              i
          ++;
                              
          if(i==30){
                                  i
          =0;
                                  monitor.beginTask(
          "generate"30);
                              }

                              monitor.worked(
          1);
                              Thread.sleep(
          100);
                          }

                          monitor.done();
                      }

                  }
          ;
                  
          new ProgressMonitorDialog(shell).run(truetrue, runnable);
                  shell.open();
                  shell.layout();
                  
          while (!shell.isDisposed()) {
                      
          if (!display.readAndDispatch())
                          display.sleep();
                  }

              }


          }

          3,rcp中后臺任務的進度條
          使用Job建立后臺任務,只需要設置job.setUser(true)進度條就出現了,和上邊一樣,進度條需要自己來控制進度。如果做一個cool的進度條,就看你如何讓進度條顯示出實際的任務進程。
          posted on 2007-08-13 10:30 冬天出走的豬 閱讀(606) 評論(0)  編輯  收藏 所屬分類: j2se
          主站蜘蛛池模板: 乌兰察布市| 建平县| 绥江县| 长治市| 夹江县| 象山县| 子长县| 定兴县| 维西| 德庆县| 吴堡县| 平谷区| 庄河市| 胶州市| 奉新县| 海门市| 夹江县| 汶上县| 嘉兴市| 博乐市| 哈巴河县| 瑞丽市| 广宁县| 宣武区| 德钦县| 亳州市| 思茅市| 龙南县| 合阳县| 汤原县| 惠水县| 赤城县| 兰考县| 酒泉市| 沈阳市| 福安市| 汝城县| 南溪县| 那坡县| 江孜县| 乃东县|