2013-06-29 162 views
1

我有一個將文件(通過ADB)複製到Android平板電腦的應用程序。這需要一些時間,所以我想顯示一個帶有不確定進度條的彈出窗口。複製任務完成後,我希望能夠停止進度欄並讓用戶關閉對話框。不確定進度條不會顯示

此刻我沒有添加額外的對話框,只是想獲得進度條的工作。我遇到的問題是進度條沒有在任務開始時顯示,但我不知道爲什麼。進度條顯示對話框何時顯示同步完成。該代碼是:

 progress = new JProgressBar(0, 100); 
     progress.setForeground(new Color(255, 99, 71)); 
     progress.setIndeterminate(true); 
     progress.setValue(0); 
     progress.setPreferredSize(new Dimension(300, 20)); 
     progress.setBounds(278, 12, 260, 20); 
     progress.setVisible(false); 
     progress.setString("Sync in progress"); 
     progress.setStringPainted(true); 
     contentPane.add(progress); 
     pushtotab = new JButton(""); 
     pushtotab.addActionListener(new ActionListener() { 


public void actionPerformed(ActionEvent arg0) { 
         if (buildpathset==1){ 
          try{ 
          setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 
          progress.setVisible(true); 
          wiredsync(); 
         }finally{ 
          JOptionPane.showMessageDialog(null, "sync complete. ",null, buildpathset); 
          setCursor(Cursor.getDefaultCursor());  
          progress.setVisible(false); 
         }}else{ 
    //warning in here later - TO Do 
       } 
       } 
       }); 

public void wiredsync(){ 

     try { 

        Process process = Runtime.getRuntime().exec("adb" + " push "+ buildpath + " " + adbtabletsync); 
        InputStreamReader reader = new InputStreamReader(process.getInputStream()); 
        Scanner scanner = new Scanner(reader); 
        scanner.close(); 
        int exitCode = process.waitFor(); 
        System.out.println("Process returned: " + exitCode); 

       } catch(IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
    }//end 

感謝您的幫助,

安迪

回答

2

pooyan有正確的想法 - 做長期運行在後臺線程中處理 - 但由於您的程序是Swing程序而不是Android程序,因此會給出錯誤的庫示例。 Swing的規範答案是在SwingWorker的doInBackground()方法中執行長時間運行的任務。

請稍等,我找到一個更好的例子...

事情是這樣:

if (buildpathset == 1) { 
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 
    progress.setVisible(true); 

    // create my SwingWorker object 
    final SwingWorker<Void, Void> myWorker = new SwingWorker<Void, Void>() { 
     protected Void doInBackground() throws Exception { 
     // here is my long running task, calling in background 
     // thread 
     wiredsync(); 
     return null; 
     }; 
    }; 

    // this allows me to be notified when the SwingWorker has 
    // finished 
    myWorker.addPropertyChangeListener(new PropertyChangeListener() { 

     @Override 
     public void propertyChange(PropertyChangeEvent pcEvt) { 
     // if the SwingWorker is done 
     if (pcEvt.getNewValue() == SwingWorker.StateValue.DONE) { 
      // notify the user 
      JOptionPane.showMessageDialog(null, "sync complete. ", 
        null, buildpathset); 
      setCursor(Cursor.getDefaultCursor()); 
      progress.setVisible(false); 

      try { 
       // one way to catch any errors that occur in 
       // SwingWorker 
       myWorker.get(); 
      } catch (InterruptedException | ExecutionException e) { 
       e.printStackTrace(); 
      } 

     } 
     } 
    }); 
    // run my SwingWorker 
    myWorker.execute(); 
} else { 
    // warning in here later - TO Do 
} 

更多關於這一點,請查看:Lesson: Concurrency in Swing

+0

謝謝。它有效(雖然我不得不改變爲JRE7,因爲我使用的是6,並且不能處理多個異常捕獲。作品謝謝! – andy

+0

@andy:如果將catch塊更改爲兩個catch塊,則可以使用JRE6。很高興它的工作,但一定要閱讀底部的鏈接,所以你可以完全理解它爲什麼工作。運氣! –