Redirect System.out to JTextArea ?

Discussion in 'Java' started by electric sheep, May 19, 2004.

  1. 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).
    :)
     
    electric sheep, May 19, 2004
    #1
    1. Advertising

  2. electric sheep

    Chris Smith Guest

    electric sheep wrote:
    > 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
     
    Chris Smith, May 19, 2004
    #2
    1. Advertising

  3. electric sheep

    Rogan Dawes Guest

    Chris Smith wrote:
    > electric sheep wrote:
    >
    >>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.
    >


    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
    --
    Rogan Dawes

    *ALL* messages to will be dropped, and added
    to my blacklist. Please respond to "nntp AT dawes DOT za DOT net"
     
    Rogan Dawes, May 20, 2004
    #3
  4. thanks
    :)
     
    electric sheep, May 20, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jan Gregor
    Replies:
    1
    Views:
    913
    Jan Gregor
    Jan 27, 2005
  2. ash
    Replies:
    1
    Views:
    1,798
    Janusch
    Mar 25, 2008
  3. Will Lichtenberger
    Replies:
    3
    Views:
    3,335
    dener
    Nov 5, 2008
  4. Erik
    Replies:
    6
    Views:
    2,078
    Mike Schilling
    Jan 22, 2010
  5. Sal
    Replies:
    1
    Views:
    915
Loading...

Share This Page