转:ProgressMonitorDialog

http://stackoverflow.com/questions/12986912/using-progressmonitordialog-in-eclipse-4-properly

 

 1 public class Progress {
 2     public static void main(String[] args)
 3     {
 4         // Create your new ProgressMonitorDialog with a IRunnableWithProgress
 5         try {
 6             // 10 is the workload, so in your case the number of files to copy
 7             IRunnableWithProgress op = new YourThread(10);
 8             new ProgressMonitorDialog(new Shell()).run(true, true, op);
 9          } catch (InvocationTargetException ex) {
10              ex.printStackTrace();
11          } catch (InterruptedException ex) {
12              ex.printStackTrace();
13          }
14     }
15 
16     private static class YourThread implements IRunnableWithProgress
17     {
18         private int workload;
19 
20         public YourThread(int workload)
21         {
22             this.workload = workload;
23         }
24 
25         @Override
26         public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException
27         {
28             // Tell the user what you are doing
29             monitor.beginTask("Copying files", workload);
30 
31             // Do your work
32             for(int i = 0; i < workload; i++)
33             {
34                 // Optionally add subtasks
35                 monitor.subTask("Copying file " + (i+1) + " of "+ workload + "...");
36 
37                 Thread.sleep(2000);
38 
39                 // Tell the monitor that you successfully finished one item of "workload"-many
40                 monitor.worked(1);
41 
42                 // Check if the user pressed "cancel"
43                 if(monitor.isCanceled())
44                 {
45                     monitor.done();
46                     return;
47                 }
48             }
49 
50             // You are done
51             monitor.done();
52         }
53 
54     }
55 }

 

posted @ 2017-01-06 16:53  kira2will  阅读(347)  评论(0编辑  收藏  举报