2012-05-07 2 views
1

스윙 워커를 사용하여 zipfile을 추출하고 추출 prosecc를 GUI의 textArea에 추가합니다. 압축 된 파일에서 하나의 항목 만 추출하고 textArea에는 아무 것도 표시되지 않았습니다.SwingWorkers가 textArea를 예상대로 업데이트하지 않는 이유는 무엇입니까?

누구든지 해결책을 제안 할 수 있습니까? 당신이

1) JTextArea.append()을 추가하여 Runnable#Thread 대신 다음 코드가 작동 SwingWorker의를 사용할 수있는이 방법 process()publish()

public class UnzipWorkers extends SwingWorker<String,Void> { 
private WebTextArea statusTextArea; 
private File archive,outputDir; 

public UnzipWorkers(WebTextArea statusTextArea,File archive,File outputDir) { 
    this.archive=archive; 
    this.outputDir=outputDir; 
    this.statusTextArea = statusTextArea; 
} 

@Override 
protected String doInBackground() throws Exception { 
     statusTextArea.append(String.valueOf(System.currentTimeMillis())); 
     try { 
      ZipFile zipfile = new ZipFile(archive); 
      for (Enumeration e = zipfile.entries(); e.hasMoreElements();) { 
       ZipEntry entry = (ZipEntry) e.nextElement(); 
       unzipEntry(zipfile, entry, outputDir); 

      } 
     } catch (Exception e) { 
      OeExceptionDialog.show(e); 
     } 

    return "Extracted successfully: " + archive.getName() + "\n"; 
} 

@Override 
protected void done() { 
    super.done(); 
    try { 
     statusTextArea.append(get()); 
     FileTreePanel.btnRefresh.doClick(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     e.printStackTrace(); 
    } 
} 

private String unzipEntry(ZipFile zipfile, final ZipEntry entry, File outputDir) { 
    String success = "Extracted failed: "+ entry + "\n"; 
    if (entry.isDirectory()) { 
     createDir(new File(outputDir, entry.getName())); 
    } 

    File outputFile = new File(outputDir, entry.getName()); 
    if (!outputFile.getParentFile().exists()){ 
     createDir(outputFile.getParentFile()); 
    } 
    try { 
     BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry)); 
     BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); 
     IOUtils.copy(inputStream, outputStream); 
     outputStream.close(); 
     inputStream.close(); 
     success="Extracted successfully: " + entry + "\n"; 
    }catch (IOException io){ 
     OeExceptionDialog.show(io); 
    }catch (NullPointerException n){ 
     OeExceptionDialog.show(n); 
    }catch (ArithmeticException a){ 
     OeExceptionDialog.show(a); 
    } 
    return success; 
} 

private void createDir(File dir) { 
    if (!dir.exists()) { 
     try { 
      dir.mkdirs(); 
     } catch (RuntimeException re) { 
      OeExceptionDialog.show(re); 
     } 
    } 
} 
} 
+1

을 제안 할하려는 JTextArea 주기적으로

3)에 추가 및 완료에(). zip 파일에 항목이 두 개 이상 있습니까? stacktrace를 인쇄 해 보셨습니까? SwingWorker에서 Swing 코드를 호출하는 것은 실제로 권장되지 않습니다. –

+0

루핑 프로세스 중에 대화 상자를 표시하지 마십시오. 특히 doInBackground() 메서드 (또는 호출하는 메서드)의 대화 상자를 표시하지 마십시오. 대신 SwingWorker.publish()에 오류 메시지를 기록한 다음 SwingWorker.process()를 사용하여 gui를 업데이트하십시오. 나는 정말로 mKorbel이 말한 것을 말하고 있습니다. – Jim

답변

5

SwingWorkerdoInBackground()에서 정기 출력, 작품이 방법으로 지정되지 않은 있습니다 , invokeLater()

2) process() 또는 publish()SwingWorker에 넣습니다. 방법은 IOStream을 넣고 난 당신이 doInBackground의 첫 번째 줄 이외의 텍스트 영역에 일을 추가 어디에 내가 볼 수없는 to use method get() for implemented reason

+0

위에서 보았지만 여전히 효과가없는 코드를 변경했습니다. textArea를 추가 한 후 updateUI와 같은 작업을해야합니까? – itro

+0

[코드 예제, JProgressBar는 JTextArea.append이고 주변 소음은 FileIO입니다] (http://stackoverflow.com/a/6114890/714968) – mKorbel

+0

@itro : 'process()'구현이 보이지 않습니다. mKorbel이 제안한대로. 이 [예제] (http://stackoverflow.com/a/4637725/230513)도 참조하십시오. – trashgod

관련 문제