aspose file tools*
The moose likes I/O and Streams and the fly likes updating a jTextBox from an iostream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "updating a jTextBox from an iostream" Watch "updating a jTextBox from an iostream" New topic
Author

updating a jTextBox from an iostream

Adam Outler
Greenhorn

Joined: Jun 24, 2011
Posts: 18
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

Joined: May 03, 2008
Posts: 4642
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Adam Outler
Greenhorn

Joined: Jun 24, 2011
Posts: 18
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

Joined: Jun 24, 2011
Posts: 18
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

Joined: Oct 27, 2005
Posts: 19720
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Adam Outler
Greenhorn

Joined: Jun 24, 2011
Posts: 18
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.
 
 
subject: updating a jTextBox from an iostream