Java redirected system output to jtext area, doesnt update until calculation is finished
Posted
by
user1806716
on Stack Overflow
See other posts from Stack Overflow
or by user1806716
Published on 2013-10-31T21:10:08Z
Indexed on
2013/10/31
21:54 UTC
Read the original article
Hit count: 148
I have code that redirects system output to a jtext area, but that jtextarea doesnt update until the code is finished running. How do I modify the code to make the jtextarea update in real time during runtime?
private void updateTextArea(final String text) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
consoleTextAreaInner.append(text);
}
});
}
private void redirectSystemStreams() {
OutputStream out = new OutputStream() {
@Override
public void write(int b) throws IOException {
updateTextArea(String.valueOf((char) b));
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
updateTextArea(new String(b, off, len));
}
@Override
public void write(byte[] b) throws IOException {
write(b, 0, b.length);
}
};
System.setOut(new PrintStream(out, true));
System.setErr(new PrintStream(out, true));
}
The rest of the code is mainly just an actionlistener for a button:
private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String shopRoot = this.shopRootDirTxtField.getText();
String updZipPath = this.updateZipTextField.getText();
this.mainUpdater = new ShopUpdater(new File(shopRoot), updZipPath);
this.mainUpdater.update();
}
That update() method begins the process of copying+pasting files on the file system and during that process uses system.out.println to provide an up-to-date status on where the program is currently at in reference to how many more files it has to copy.
© Stack Overflow or respective owner