• Post Reply Bookmark Topic Watch Topic
  • New Topic

Redirecting System.out to JTextArea problem

 
R Firoz
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an external program that does some stdout and stderr. The task is to redirect the stdout and stderr to gui JTextArea. So far,I have the following class:



Then later on when I init my JTextArea, I do the following to do the redirection.



The problem is, it waits for all the processes in the external program to complete executing then update the JTextArea.

Any input is appreciated.

Thanks,
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things you can try:

1) Turn autoFlush on in the PrintStream:
PrintStream textWriter = new PrintStream(new JTextAreaOutputSteam(myJTextArea), true);
Actually, I doubt that will help but it's easy so might be worth a try.

2) Have your JTextAreaOutputSteam class extend PrintStream directly.
This will mean you have to implement 20 methods or so, but they should be pretty simple.

Btw, JTextArea.append() is documented to be thread-safe so you shouldn't need the SwingUtilities.invokeLater() bit.

[edit: fix bad hyperlink]
[ February 27, 2008: Message edited by: Brian Cole ]
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's this "external program" you mentioned? Is it a different process than the one where your posted code is located?
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by R Firoz:
The problem is, it waits for all the processes in the external program to complete executing then update the JTextArea.


This could actually be a threading issue. If the Event Dispatching thread is waiting on this external process of yours, then the JTextArea won't be able able to repaint itself until it's done waiting.

You don't give details, but if this might be the case then make sure it's another thread that is doing the waiting.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!