Reputation: 900
I'd like to append a character to a text area in real-time.
I've got a buffered reader called br, and it's reading a very long process to a text area.
cmd=ArrayOfCommandsWhichWorkProperly
Runtime run = Runtime.getRuntime() ;
pr = run.exec( cmd );
BufferedReader buf = new BufferedReader( new InputStreamReader(pr.getInputStream() ) ) ;
while ( (c=br.read()) >-1 ) {
String s=Character.toString((char)br.read());
//Why is this text box not updating?
jTextArea2.append(s);
}
It seems that there must be an acellerator in the way or something... The problem is that it does not update until the entire processs is complete. What can I add to make the text box update?
I tried Thread.sleep and Thread.yeild. It seems that the jTextArea is just accumulating and not updating until the process is complete.
Upvotes: 0
Views: 1739
Reputation: 324098
I tried Thread.sleep and Thread.yeild.
Never use Thread.sleep() when code is executing on the EDT.
It seems that the jTextArea is just accumulating and not updating until the process is complete.
Yes. You are blocking the EDT from repainting. Read the section from the Swing tutorial on Concurrency for a more detailed explanation and a simple solution using a Swing Worker.
Upvotes: 2