Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Real Time program updating to screen?  RSS feed

 
Andy Lukavich
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a program that helps convert XML files into the specified word document, but have run into a small snag. We want to be able to see the file conversion process working in real time, but I can't figure how to get it to update the JTextArea in my program.

Here is the part of the code I am referring to:

public void ExecCommand() {

Cursor hourglassCursor = new Cursor(Cursor.WAIT_CURSOR);
setCursor(hourglassCursor);

try {

Process p = Runtime.getRuntime().exec("cmd.exe /E:1900 /C C://test ");

BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));

String s = null;

while ((s = stdInput.readLine()) != null)
{
System.out.println(s);

}
}


catch (IOException e)
{ System.err.println(e); }

Cursor normalCursor = new Cursor(Cursor.DEFAULT_CURSOR);
setCursor(normalCursor);

if (logCheck == true)
{createLog(); }
else
{ done();}

}

The System.out.println(s) works great, but obviously only prints to the Terminal window. I tried adding a displayText.append(s) command into the while loop, but it only updates after the whole program has run...not during the process.

Any suggestions?
 
Joe Ess
Bartender
Posts: 9425
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to run your process logic in a new thread in order to free up the Swing event thread for updating the gui.
We have a FAQ entry that talks about a situation very similar to yours.
There's also examples in the Java Tutorial chapter on concurrency in Swing.
 
Andy Lukavich
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, this is exactly what i was looking for.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!