为什么实时输出到JTextArea不显示?

时间:2012-05-04 10:55:42

标签: java swing console jtextarea

我使用JTextArea来显示解压缩文件的状态。 但由于某种原因,它不显示附加文本。 任何人都可以提出解决方案吗?

public class UnzipFile extends Thread{
private static FtpTabPanel panel;
private File archive,outputDir;

public UnzipFile(File archive, File outputDir, FtpTabPanel panel) {
    UnzipFile.panel = panel;
    this.archive = archive;
    this.outputDir = outputDir;
}

@Override
public void run() {
    super.run();
    unzipArchive();
}

public void unzipArchive() {
    try {
        ZipFile zipfile = new ZipFile(archive);
        for (Enumeration e = zipfile.entries(); e.hasMoreElements(); ) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            unzipEntry(zipfile, entry, outputDir);
        }
        panel.statusTextArea.append(String.valueOf(System.currentTimeMillis()));

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

private void unzipEntry(ZipFile zipfile, final ZipEntry entry, File outputDir)  {
    if (entry.isDirectory()) {
        createDir(new File(outputDir, entry.getName()));
        return;
    }

    File outputFile = new File(outputDir, entry.getName());
    if (!outputFile.getParentFile().exists()){
        createDir(outputFile.getParentFile());
    }

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            panel.statusTextArea.append("Extracting: " + entry + "\n");
        }
    });

    try {
    BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
    IOUtils.copy(inputStream, outputStream);
    outputStream.close();
    inputStream.close();    
    }catch (IOException io){
        OeExceptionDialog.show(io);
    }catch (NullPointerException n){
        OeExceptionDialog.show(n);
    }catch (ArithmeticException a){
        OeExceptionDialog.show(a);
    }
}
}

在下面的代码中,我使用SwingWorkers,但它只解压缩zip文件中的一个项目,并且jtextArea中没有任何内容

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();
                String status = unzipEntry(zipfile, entry, outputDir);
                return status;
            }
        } catch (Exception e) {
            OeExceptionDialog.show(e);
        }

    return null;  
}

@Override
protected void done() {
    super.done();
    try {
        statusTextArea.append( get() + "\n");
        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);
        }
    }
}
}

2 个答案:

答案 0 :(得分:1)

从这条线来判断:

panel.statusTextArea.append(String.valueOf(System.currentTimeMillis()));

您正在EDT上运行代码,否则您将获得IllegalThreadAccess异常。因此,实际上,您的整个提取过程都是在处理单个事件时完成的。您更新TextArea的请求只是被推送到事件队列并等待,直到您完成“处理”触发提取代码的事件。

您必须在专用线程上运行代码(使用SwingWorker)。

答案 1 :(得分:0)

您是否尝试移动

panel.statusTextArea.append(String.valueOf(System.currentTimeMillis()));

在循环内部?