Reputation: 401
I have a JTextArea inside a class that I want to update dynamically. Currently it is only displaying the text I append to it after all the processing is done. I have tried to implement the following to fix it:
public NewConsole(){
initComponents();
}
public void write(final String s){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textarea.append(s);
}
});
}
Console gets instantiated in a parent class as:
protected NewConsole console = new NewConsole();
and to output to it, all the children call:
console.write("Append this..");
EDIT: Here's some more information:
public abstract class Parent{
protected NewConsole console = new NewConsole();
public Parent(){}
protected abstract int doSomething();
}
public class Child extends Parent{
public Child(){
console.write("I want this to update dynamically");
doSomething();
console.write("And this..");
}
public int doSomething(){
//Quite intensive processing here
}
}
Upvotes: 1
Views: 1031
Reputation: 159754
The intensive processing done in doSomething
is blocking the EDT
, preventing UI updates. Use a SwingWorker instead to perform this functionality.
Use execute to start the worker. Move any required calls to console.write
to either doInBackground or done.
Upvotes: 3
Reputation: 13103
You might try calling invokeAndWait() in place of invokeLater(), but in fact there is not enough information to be sure of an answer here.
I think of invokeLater() as "put this in your queue of things to do", and invokeAndWait() as "put this in the queue of things to do, and I'll suspend while you do them". I don't know if this change will fix your problem, but it seems like something to try based on what you've told us.
Upvotes: 0