2011-06-21 15 views
2

私はJavaを初めて使用しています。 ファイル の円弧を作るプログラムを書いていますが、進捗状況を示す棒グラフを使用して、ジップの進捗状況を表示したい 私のコードはありますが、作業は完了しません。 は作業完了時に100%Javaファイルに進捗バー

 getContentPane().add(progressBar); 
    progressBar.setBorder(new CompoundBorder(null, null)); 
    springLayout.putConstraint(SpringLayout.EAST, progressBar, 0, SpringLayout.EAST, messageLabel); 
    springLayout.putConstraint(SpringLayout.WEST, progressBar, 0, SpringLayout.WEST, messageLabel); 
    springLayout.putConstraint(SpringLayout.SOUTH, progressBar, 165, SpringLayout.NORTH, getContentPane()); 
    springLayout.putConstraint(SpringLayout.NORTH, progressBar, 5, SpringLayout.SOUTH, uploadLocalButton); 
    progressBar.setValue(0); 
    progressBar.setMaximum(100); 
    progressBar.setMinimum(0); 
    progressBar.setStringPainted(true); 
... 
public void doZip(String filename,String outFilename) 
{ 
    try { 
     byte[] dataBytes = new byte[1024]; 
     File file = new File(outFilename); 
     file.createNewFile(); 
     FileOutputStream fos = new FileOutputStream(file); 
     ZipOutputStream zos = new ZipOutputStream(fos); 
     File fil = new File(filename); //"Users/you/image.jpg" 
     long length= fil.length(); 
     System.out.println(length); 
     zos.putNextEntry(new ZipEntry(fil.getName()));//image.jpg 
     FileInputStream in = new FileInputStream(filename); 
     int current=0; 
     int len; 

     while ((len = in.read(dataBytes)) > 0) 
     { current += len; 
     messageLabel.setText(Integer.toString(current)); 
     System.out.println(current); 
     final double progres=(((double)current/(double)length)*100); 
     progressBar.setValue((int)progres);    
      zos.write(dataBytes, 0, dataBytes.length); 
     } 

     zos.closeEntry(); 
     zos.close(); 
     in.close(); 
}catch (IOException e) { 
     JOptionPane.showMessageDialog(null, 
       "Access denied. File or patch not exist or in currently in use.","Error", 
       JOptionPane.ERROR_MESSAGE); 
     e.printStackTrace(); 

    }} 

誰かが問題を抱えていると知っているかもしれませんか?


私はボタンを押すことでdoZipを呼び出します。 私はこの

while ((len = in.read(dataBytes)) > 0) 
     { current += len; 
     zos.write(dataBytes, 0, dataBytes.length); 
     final double progres=(((double)current/(double)length)*100); 
     try{ 
      SwingUtilities.invokeLater(new Runnable(){ 
       public void run(){ 
       progressBar.setValue((int)progres); 
       } 
      }); 
      java.lang.Thread.sleep(100); 
     }catch(InterruptedException e){;} 

を試してみましたが、それはまたdoesent仕事、唯一はるかに長いジッパーopperationのプロセスになります。

+0

スレッド内の 'doZip'への呼び出しをトリックする必要があります – dcn

答えて

2

AWTスレッドのどこかからdoZipを呼び出していると仮定します(クリックなどのアクションハンドラによって直接(in)と呼ばれます)。したがって、間に応じて進捗状況を設定しても、アクション(ジップ)が完了するまでUIは更新されません。

別のスレッドでdoZipを開始するのが1つの方法です(一般的に、 AWTスレッドではコストのかかる操作ですが、基本的な 'アクション処理'のみを行い、そこから長時間実行される操作をトリガーします)。

関連する問題