Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

updating a jTextBox from an iostream

 
Adam Outler
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello... I came to this site because of this thread http://www.coderanch.com/t/274714/Streams/java/output-JTextArea but, I noticed it was 10 years old...

Here's what I have.


The problem is that the text box does not update until the end of the process

How can I make view.jTextArea1 update in real time?
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Adam and welcome to the Ranch! First, it's good that you started a new thread, some of the classes and methods you would use today weren't around when that question was asked... SwingWorker comes to mind. Secondly, what's a jTextBox?

I guess you need to go through this tutorial.
 
Adam Outler
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Hi Adam and welcome to the Ranch! First, it's good that you started a new thread, some of the classes and methods you would use today weren't around when that question was asked... SwingWorker comes to mind.


Thanks alot!


Secondly, what's a jTextBox?

Ok, you got me.. I havn't been faithful to Java. I did coding with Visual basic. But it was a long time ago and she means nothing to me... *sniff* *sniff*.


I guess you need to go through this tutorial.
I really appreciate the help.


I started reading that last night. From what It looks like I need to override the swing worker thread and then start it. to have it do what I want.
 
Adam Outler
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I started working on the problem and I stopped before I got to implement swing worker... it seems to run fine in a runnable class.



Why would one use swing worker? What is the advantage?
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All updates to the JTextArea should be done on the Event Dispatcher Thread. The Javadoc says that JTextArea.append is thread-safe but it isn't; the Java 7 Javadoc fixes that.

With SwingWorker you could do the bulk of the work in doInBackground(). When you read a line you don't append it directly but instead you publish it. You override the process method to append all lines you've published.

With a regular Runnable / Thread you would need to use EventQueue.invokeLater (or SwingUtilities.invokeLater; this merely calls the former) for each line you need to append.


Now, I see two problems with your code:
1) readLine strips away the line breaks, so those will be missing from the JTextArea. You need to add them manually again.

2) you now read the input and error streams consecutively. This could still lead to deadlock when the process' error stream buffer becomes full and the process just stops (see When Runtime.exec() won't). You really need to a) read them concurrently, or b) use ProcessBuilder and call redirectErrorStream(true) on it, in which case you can ignore the error stream afterwards.
 
Adam Outler
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok... once again... you guys got me... That's not my actual code.. I was trying to keep it simple.

I've got quite a bit more going on in the background and it looks kinda sloppy... let me edit the first post in case someone reads this and tries to copy it directly.

I do want the error stream to come last. This code initiates a firmware flash. If there's a problem, the errors would be good to see at the end.

I'm still unclear as to why I would want to use the Worker thread instead of just implementing a new runnable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic