如何确保在资源密集型代码之前/期间/之后更新文本区域?

时间:2015-01-18 02:42:58

标签: java swing logging event-dispatch-thread

我有一个带有actionPerformed事件的Swing按钮可以下载文件,当点击它时会调用该文件:

private static void updateTextArea(final String text) {
    textArea.append(text + "\n");
    textArea.setCaretPosition(textArea.getDocument().getLength());
}

然后打电话:

public static void download(String fileName, String url) throws IOException {
     URL link = new URL(url);
     InputStream in = new BufferedInputStream(link.openStream());
     ByteArrayOutputStream out = new ByteArrayOutputStream();
     byte[] buf = new byte[1024];
     int n = 0;
     while (-1!=(n=in.read(buf)))
     {
        out.write(buf, 0, n);
     }
     out.close();
     in.close();
     byte[] response = out.toByteArray();

     FileOutputStream fos = new FileOutputStream(fileName);
     fos.write(response);
     fos.close();
}

文件按预期下载,我的问题是textArea在文件下载完成后更新,但我希望它在我{cal updateTextArea("")后立即更新。

如何确保在长时间运行的流程之前更新文本区域?

0 个答案:

没有答案