2013-06-05 129 views
1

我試圖在我的應用程序中添加一個進度條,該進度條根據命令行或Java代碼進行,我的應用程序是一種設置或安裝的另一種Etaps程序。在命令行或複製任務中進度的進度條

我發現所有進度條都不與複製任務交互,這意味着我不知道複製任務何時完成。

我想我的進度條,在這些線路的功能進步命令:

try { 
      Runtime.getRuntime().exec("cmd.exe /k mkdir \""+dossier+"\\results\" &&" 
        + " mkdir \""+dossier+"\\results\\basededonnees\" &&" 
        + " mkdir \""+dossier+"\\results\\ordonnances\" &&" 
        + " mkdir \""+dossier+"\\results\\piecesjoins\""); 

      Runtime.getRuntime().exec("cmd.exe /k mkdir \""+dossier+"\\basededonnees\" &&" 
        + " echo CREATE DATABASE "+nomBase+"; >> \""+dossier+"\\basededonnees\\CreerDB.sql\""); 

      Runtime.getRuntime().exec("cmd.exe /k xcopy apps\\lib \""+dossier+"\\lib\" &&" 
        + " set path=%path%;C:\\wamp\\bin\\mysql\\mysql5.5.20\\bin &" 
        + " set path=%path%;C:\\wamp\\bin\\mysql\\mysql5.5.24\\bin &" 
        + " mysql -u root < \""+dossier+"\\basededonnees\\CreerDB.sql\""); 

    } catch (IOException ex) { 
     JOptionPane.showMessageDialog(null, "Une erreur est survenue \n" + ex.getMessage(), "Information", JOptionPane.INFORMATION_MESSAGE); 
    } 

檔案= 'C:\ Program Files文件\ NameOfMyApps' nomBase = 'NameOfMyDataBase'

當所有的命令和複製任務完成我希望酒吧達到100%。

預先感謝您,並遺憾的語言,因爲我不metrized一個良好的英語

更好地解釋這裏是截圖: enter link description here

http://www.4shared.com/download/-b2S2bFN/apps.jpg?tsid=20130605-124016-afcd18b4

回答

1

喜見下面的例子

package com.test; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 

public class Copy { 

    private static final String PING_LOCALHOST = "ping localhost"; 

    /** 
    * @param args 
    * @throws InterruptedException 
    */ 
    public static void main(String[] args) throws InterruptedException { 
     // TODO Auto-generated method stub 
     ProgressBar progressBar = new ProgressBar(); 
     progressBar.showProgress(); 
     // first command 
     runCommand(progressBar, PING_LOCALHOST, 25); 
     runCommand(progressBar, PING_LOCALHOST, 50); 
     runCommand(progressBar, PING_LOCALHOST, 75); 
     runCommand(progressBar, PING_LOCALHOST, 100); 

    } 

    private static void runCommand(ProgressBar progressBar, String command, int barValue) throws InterruptedException { 
     CopyThread copyThread = new Copy().new CopyThread(command); 
     copyThread.start(); 
     // wait for thread to die 
     copyThread.join(); 
     progressBar.updatePercent(barValue); 
    } 

    private class CopyThread extends Thread { 
     private String cmd; 

     public CopyThread(String command) { 
      this.cmd = command; 
     } 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      try { 
       String line; 
       Process p = Runtime.getRuntime().exec(cmd); 
       BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); 
       while ((line = input.readLine()) != null) { 
        System.out.println(line); 
       } 
       input.close(); 
      } catch (Exception err) { 
       err.printStackTrace(); 
      } 
     } 

    } 

} 

進度

package com.test; 

import java.awt.Color; 
import java.awt.Font; 

import javax.swing.JFrame; 
import javax.swing.JProgressBar; 

public class ProgressBar { 

    private JFrame progressFrame; 
    private JProgressBar progressBar; 

    /** 
    * Create the application. 
    */ 
    public ProgressBar() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 

     progressFrame = new JFrame(); 
     progressFrame.setTitle("Test progress bar"); 
     progressFrame.setResizable(false); 
     progressFrame.setBounds(100, 100, 640, 79); 
     progressFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     progressFrame.getContentPane().setLayout(null); 

     progressBar = new JProgressBar(); 
     progressBar.setFont(new Font("Tahoma", Font.PLAIN, 16)); 
     progressBar.setBackground(new Color(255, 255, 255)); 
     progressBar.setIndeterminate(false); 
     progressBar.setStringPainted(true); 
     progressBar.setForeground(new Color(0, 128, 0)); 
     progressBar.setBounds(0, 0, 634, 52); 

     progressFrame.getContentPane().add(progressBar); 
    } 

    public void showProgress() { 
     initialize(); 
     progressFrame.setVisible(true); 
    } 

    public void closeProgress() { 
     progressFrame.dispose(); 
     progressFrame.setVisible(false); 
    } 

    public void updatePercent(int value) { 
     progressBar.setValue(value); 
    } 
} 
+0

的一切,用於已經可以看到每JProgressBar的方法必須被包裹在了invokeLater – mKorbel

+0

非常感謝你這是爲我工作!但進度條依賴於線程(sleep(2000);)不依賴於命令,我不知道是否可能。顯然這個解決方案對我來說只是我必須測量(睡眠(對複製的時間估計);)再次感謝你。 – Mouad

+0

不,你不應該依賴線程睡眠那隻適用於虛擬....如果你遠程線程睡眠,那麼它將取決於複製所花費的時間 – Makky