Redirect System.out to JTextArea ?

E

electric sheep

I thought this would be easier than it is.
Do I need to use a PiperReader maybe ?

I'd also like to redirect System.err to the same JTextArea.
I'll probably need some sort of stream "multiplexer" for this.

I'm sort of new to all this as they don't teach us anything useful in
school (I do know a little ml and prolog, however).
:)
 
C

Chris Smith

electric said:
I thought this would be easier than it is.
Do I need to use a PiperReader maybe ?

You need a PipedInputStream/PipedOutputStream pair (remember, System.out
is an OutputStream, not a Writer), so you call System.setOut for the
ouput half. You then need an InputStreamReader to convert the bytes
into characters (and this is pretty much the one and only place where
you *do* want to use the system default encoding), and a thread to read
from that InputStreamReader and append the results to the JTextArea.
Conveniently, JTextArea.append may be called from a non-event thread,
which saves you a lot of extra trouble.
I'd also like to redirect System.err to the same JTextArea.
I'll probably need some sort of stream "multiplexer" for this.

Nah, you ought to be able to use System.setOut and System.setErr with
the same stream, which would cause it to be used for both of the two
standard OuputStream designations.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

Rogan Dawes

Chris said:
You need a PipedInputStream/PipedOutputStream pair (remember, System.out
is an OutputStream, not a Writer), so you call System.setOut for the
ouput half. You then need an InputStreamReader to convert the bytes
into characters (and this is pretty much the one and only place where
you *do* want to use the system default encoding), and a thread to read
from that InputStreamReader and append the results to the JTextArea.
Conveniently, JTextArea.append may be called from a non-event thread,
which saves you a lot of extra trouble.




Nah, you ought to be able to use System.setOut and System.setErr with
the same stream, which would cause it to be used for both of the two
standard OuputStream designations.

I created a DocumentOutputStream that simply appends (all text written
to the OutputStream) to (the Document supplied in the Constructor), and
fires appropriate events.

This seems to me to be simpler than your suggestion (no aditional Threads).

Usage was similar to:

Document doc = myTextComponent.getDocument();
System.setOut(new PrintStream(new DocumentOutputStream(doc)));

It was simpler to implement 2 methods for OutputStream than all the
methods of PrintStream ;-)

You can google for DocumentOutputStream, if you like. It was posted here
not too long ago, and is also in the WebScarab source on sourceforge.

Rogan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top