2017-07-04 40 views
0

我已經實現了SWT嚮導應用程序,在此應用程序中,我以編程方式執行解壓縮文件,並且在解壓縮期間需要一個進度條。如果您使用的是JFace的Wizard,你可以使用內置的進度監視器中,請找到解壓下面的代碼,需要在SWT嚮導中解壓縮時的進度條由java

public static void extract(File zipfile, File outdir) 
     { 
     try 
     { 
      ZipInputStream zin = new ZipInputStream(new FileInputStream (zipfile)); 
      ZipEntry entry; 
      String name, dir; 
      while ((entry = zin.getNextEntry()) != null) 
      { 
      name = entry.getName(); 
      if(entry.isDirectory()) 
      { 
       mkdirs(outdir,name); 
       continue; 
      } 
      /* this part is necessary because file entry can come before 
      * directory entry where is file located 
      * i.e.: 
      * /foo/foo.txt 
      * /foo/ 
      */ 
      dir = dirpart(name); 
      if(dir != null) 
       mkdirs(outdir,dir); 

      extractFile(zin, outdir, name); 

      } 

      zin.close(); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
     } 

在嚮導頁中我把這個方法就是這樣,

btnUnzip.addListener(SWT.Selection, new Listener() { 

      @Override 
      public void handleEvent(Event event) { 

        File file = new File(text.getText()); 
        File file1 = new File(textToSaveUnzipFile.getText()); 
        UnzipUtility.extract(file, file1); 
     } 
    } 
+0

你是什麼意思是'SWT嚮導'?這是使用JFace'Wizard' /'WizzrdDialog' /'WizardPage'類(它有一個內置的進度監視器),還是隻是簡單的'SWT'? –

+0

在SWT項目中使用wizardDialog – Biswabir

+0

感謝您的回覆,如果我擴展了WizardPage類,那怎麼可能。 – Biswabir

回答

0

嚮導。

在你的嚮導類調用構造函數:

setNeedsProgressMonitor(true); 

要顯示正在進行的呼叫

getContainer().run(true, true, runnable); 

此調用可以在WizardWizardPage

其中runnable是實施IRunnableWithProgress的類。這個類的run方法看起來像這樣:

@Override 
public void run(final IProgressMonitor monitor) 
    throws InterruptedException 
{ 
    monitor.beginTask("Title", .. number of work steps ..); 

    try 
    { 
    while (not finished) { 
     ... do a small amount of work 

     // Update progress 
     monitor.worked(1); 
    } 
    } 
    finally 
    { 
    monitor.done(); 
    } 
} 
+0

它的工作非常感謝... – Biswabir

+0

如果我在開始任務100中設置工作步驟並在此之後使用monitor.worked(100),我有問題否定進度條。在解壓縮進度條時顯示100,然後完成解壓縮功能。 – Biswabir

+0

如果你想取得進展,表明你必須在每一位之後以小數位完成調用「工作」的工作。如果你不能做到這一點,你不能有動畫進展。 –