Jump to content

Howto: Redirect System.out/system.err To Jtextarea Control

  • Please log in to reply
2 replies to this topic

#1 buddybob


    Advanced Member

  • Members
  • PipPipPip
  • 452 posts
  • Gender:Male
  • Location:Bangalore
  • Interests:Java, Airplanes, Trance Music, Movies...blah blah

Posted 06 February 2006 - 06:35 AM

Hi all,

Here is a solution which can help you redirect standard output stream (System.out) and standard error stream (System.err) outputs to a javax.swing.JTextArea control. These can be helpful if you want to develop applications which execute external programs and capture their output within your program (Similar to JCreator  cool.gif )

* @(#) TextAreaOutputStream.java

import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;

* An output stream that writes its output to a javax.swing.JTextArea
* control.
* @author  Ranganath Kini
* @see      javax.swing.JTextArea
public class TextAreaOutputStream extends OutputStream {
    private JTextArea textControl;
     * Creates a new instance of TextAreaOutputStream which writes
     * to the specified instance of javax.swing.JTextArea control.
     * @param control   A reference to the javax.swing.JTextArea
     *                  control to which the output must be redirected
     *                  to.
    public TextAreaOutputStream( JTextArea control ) {
        textControl = control;
     * Writes the specified byte as a character to the
     * javax.swing.JTextArea.
     * @param   b   The byte to be written as character to the
     *              JTextArea.
    public void write( int b ) throws IOException {
        // append the data as characters to the JTextArea control
        textControl.append( String.valueOf( ( char )b ) );

The TextAreaOutputStream extends the java.io.OutputStream class and overrides its write( int ) method overload, this class uses a reference to a javax.swing.JTextArea control instance and then appends output to it whenever its write( int b ) method is called.

To use the TextAreaOutputStream class, u shud use:

// Create an instance of javax.swing.JTextArea control
JTextArea txtConsole = new JTextArea();

// Now create a new TextAreaOutputStream to write to our JTextArea control and wrap a
// PrintStream around it to support the println/printf methods.
PrintStream out = new PrintStream( new TextAreaOutputStream( txtConsole ) );

// redirect standard output stream to the TextAreaOutputStream
System.setOut( out );

// redirect standard error stream to the TextAreaOutputStream
System.setErr( out );

// now test the mechanism
System.out.println( "Hello World" );

Here is a screenshot of a sample program I have used this technique in:

IPB Image

Your comments are invited.

Try it out. Hope it helps!  smile.gif
Best Regards,
Ranganath Kini
Sun Certified Java Programmer

#2 jeef


    Advanced Member

  • Members
  • PipPipPip
  • 344 posts
  • Location:New Zealand

Posted 09 February 2006 - 10:15 PM

haha, nice :)
no point making your program so simple a dummy can use it cause the universe just keeps making a better dummy!

#3 jdeegan



  • Members
  • Pip
  • 1 posts

Posted 08 March 2008 - 01:37 PM


Would it be okay with you if I use this (attributed with your name and this URL) in some code that is open source and produced by a non-profit organization?