简体   繁体   中英

Why Output to JTextArea in real-time doesn't show up?

I use JTextArea to show status of unzipping file. But for a reason it does not display the appended text. Can any one suggest a solution?

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);
    }
}
}

In the below code i use SwingWorkers but it unzip just one item from zip file and nothing appears in the 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);
        }
    }
}
}

Judging by this line:

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

you are running your code on the EDT, otherwise you'd get an IllegalThreadAccess exception. So, in effect, your whole extraction procedure is done as the handling of a single event. Your requests to update the TextArea are just being pushed to the event queue and wait there until you have done "handling" the event that triggered the extraction code.

You must run your code on a dedicated thread (use SwingWorker).

Did you try to move the

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

inside of the loop?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM