I have a Swing button with an actionPerformed
event that downloads a file, when it is clicked it calls:
private static void updateTextArea(final String text) {
textArea.append(text + "\n");
textArea.setCaretPosition(textArea.getDocument().getLength());
}
And then calls:
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();
}
The file downloads as intended, my problem is that the textArea updates AFTER the file is finished downloading, but I want it to update as soon as I cal updateTextArea("")
.
How to ensure the text area is updated before the long running process?