I have a java program that does file manipulation and I have a GUI class that has a JTextArea that has console output redirected to it. I am trying to get the SwingWorker in a different class to publish to that JTextArea but I can't seem to get it to work properly. In my GUI class, I have the following methods:
public ShopUpdaterGUI() {
initComponents();
redirectSystemStreams();
}
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));
}
And then in my other class that does the work I have this:
public void update(){
(updateTask = new UpdateTask()).execute();
}
private class UpdateTask extends SwingWorker<Void, String>{
@Override
protected Void doInBackground() {
try{
publish("Creating backup...");
mainBackup.createBackup();
publish("Setting restore point...");
setRestorePoint();
publish("Applying update...");
UPDHandler.unZipUpdate();
saveModifiedFilesList();
}catch(IOException ex){
ex.printStackTrace();
}catch(ClassNotFoundException ex){
ex.printStackTrace();
}finally{
publish("Update Complete!");
}
return null;
}
}
Edit: here my process method:
protected void process(List<String> updates){
String update = updates.get(updates.size() - 1);
System.out.println(update);
}
This works sometimes but other times it completely skips over one of the publish() calls. For example, when it reaches publish("Setting restore point...") that would never actually show up on the JTextArea, and instead it would just skip over to "Applying Update..."
How do I get this to publish and update the JTextArea exactly when I tell it to?