Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Redirecting System.out.println()

Thread view: 
Ike - 05 Feb 2006 11:26 GMT
Is anyone familiar with a way to redirect System.out.println()? I'd like to
redirect it to a JTextArea if possible. Thanks, Ike
Roedy Green - 05 Feb 2006 12:37 GMT
>Is anyone familiar with a way to redirect System.out.println()? I'd like to
>redirect it to a JTextArea if possible. Thanks, Ike

I think it is called System.setOut IIRC. But it wont let you direct to
a JTextArea. It does not implement any sort of stream interface.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Daniel Dyer - 05 Feb 2006 12:43 GMT
>> Is anyone familiar with a way to redirect System.out.println()? I'd  
>> like to
>> redirect it to a JTextArea if possible. Thanks, Ike
>
> I think it is called System.setOut IIRC. But it wont let you direct to
> a JTextArea. It does not implement any sort of stream interface.

But there is, of course, nothing to stop you from writing an OutputStream  
implementation that appends to a JTextArea.

Dan.

Signature

Daniel Dyer
http://www.dandyer.co.uk

Thomas Fritsch - 06 Feb 2006 00:41 GMT
>>> Is anyone familiar with a way to redirect System.out.println()? I'd
>>> like to
[quoted text clipped - 5 lines]
> But there is, of course, nothing to stop you from writing an OutputStream
> implementation that appends to a JTextArea.

I guess this wheel has already been invented several times.
When people implement such thing, they tend to call their class
"TextAreaOutputStream".
Therefore you should search the "comp.lang.java.*" groups for
"TextAreaOutputStream".
See http://groups.google.com

Signature

"TFritsch$t-online:de".replace(':','.').replace('$','@')

Frank D. Greco - 05 Feb 2006 18:36 GMT
Roedy Green <my_email_is_posted_on_my_website@munged.invalid> sez:

>>Is anyone familiar with a way to redirect System.out.println()? I'd like to
>>redirect it to a JTextArea if possible. Thanks, Ike
>
>I think it is called System.setOut IIRC. But it wont let you direct to
>a JTextArea. It does not implement any sort of stream interface.

I asked the Swing team for this (and integration with NIO) a *long* time ago.
No response.  [I got the same response when I asked for a skinnable PLAF back in
1999]

It'd be pretty easy to write QnD wrappers for monitoring file logs for
enterprise app developers who aren't wizards.

Frank G.
+==========================================+
| Crossroads Technologies Inc.             |
| www.CrossroadsTech dot com               |
| fgreco at REMOVE!cross!roads!tech!dot com|
+==========================================+
Ian Mills - 05 Feb 2006 13:32 GMT
> Is anyone familiar with a way to redirect System.out.println()? I'd like to
> redirect it to a JTextArea if possible. Thanks, Ike

Why exactly do want to do this? Surly it would make more sense to send
your output directly to the JTextArea rather than System.out
Frank D. Greco - 05 Feb 2006 18:29 GMT
Ian Mills <news@siteone.free-online.co.uk> sez:

>> Is anyone familiar with a way to redirect System.out.println()? I'd like to
>> redirect it to a JTextArea if possible. Thanks, Ike
>>
>Why exactly do want to do this? Surly it would make more sense to send
>your output directly to the JTextArea rather than System.out

    Perhaps Ike wanted to capture the output of an exec()-ed pgm?

    Frank G.
Ike - 05 Feb 2006 19:07 GMT
Just looking to capture what is output to the plugin console without having
to try to talk people through how to get it out of there! -Ike

> Ian Mills <news@siteone.free-online.co.uk> sez:
>
[quoted text clipped - 7 lines]
>
> Frank G.
steve - 05 Feb 2006 21:53 GMT
> Just looking to capture what is output to the plugin console without having
> to try to talk people through how to get it out of there! -Ike
[quoted text clipped - 11 lines]
>>
>> Frank G.

you need to write a wrapper. something like this.
yes i know this code is a piece of crap, and a lot of it is redundant(i  
spent some time getting it to work), but it works.

package com.Errors;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream;

import java.util.*;
import java.util.regex.*;

//this should only be used with the log4j package
//it is to trap any errors that might be directed to the console screen.
public class consoleIntercept extends PrintStream {
   private PrintStream console = System.out;
   private PrintStream err = System.err;

   public consoleIntercept() {
       super(System.out, true); // Autoflush
       System.setOut(this);
       System.setErr(this);

     

   }

//error_stuff  is the package  that  log4j's  the sh.t sent to it.

   // public PrintStream getConsole() { return console; }
   public void dispose() {
       System.setOut(console);
       System.setErr(err);

   
   }

   // Override all possible print/println methods to send
   public void print(String x) {
       console.print(x); // keep it to the console as well
       Error_stuff.handleError(x);
   }

   public void println(String x) {
       console.println(x); // keep it to the console as well
       Error_stuff.handleError(x);
   }
 public void print(Object x) {
   Error_stuff.handleError(x.toString());

 }
 public void println(Object x) {

   Error_stuff.handleError(x.toString());
 }
  /*
   public void print(boolean x) {
     console.print(x);

   }
   public void println(boolean x) {
     console.println(x);
   }
   public void print(char x) {
     console.print(x);

   }
   public void println(char x) {

     console.println(x);
     fout.println(x);
   }
   public void print(int x) {
     console.print(x);

   }
   public void println(int x) {

     console.println(x);
     fout.println(x);
   }
   public void print(long x) {
     console.print(x);

   }
   public void println(long x) {

     console.println(x);
     fout.println(x);
   }
   public void print(float x) {
     console.print(x);

   }
   public void println(float x) {

     console.println(x);
     fout.println(x);
   }
   public void print(double x) {
     console.print(x);

   }
   public void println(double x) {

     console.println(x);
     fout.println(x);
   }
   public void print(char[] x) {
     console.print(x);

   }
   public void println(char[] x) {

     console.println(x);
     fout.println(x);
   }

 
   public void println() {
     if(false) console.print("println");

     console.println();
     fout.println();
   }
 public void write(int x) {
 Error_stuff.handleError(x.toString());
 }

    public void
     write(byte[] buffer, int offset, int length) {
       console.write(buffer, offset, length);
     
     }
     public void write(int b) {
       console.write(b);
       fout.write(b);
}
*/
}
Ranganath Kini - 06 Feb 2006 05:58 GMT
> Is anyone familiar with a way to redirect System.out.println()? I'd like to
> redirect it to a JTextArea if possible. Thanks, Ike

Dear Ike,

I have written a small class which can be used to redirect output to a
JTextArea control. Here is the code:

/*
* @(#) 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 character to the JTextArea control
       textControl.append( String.valueOf( ( char )b ) );
   }
}

To use the TextAreaOutputStream, use the following code:

JTextArea txtConsole = new JTextArea();
PrintStream out = new PrintStream(
                                 new TextAreaOutputStream( txtConsole
) );
System.setOut( out );
System.setErr( out );
System.out.println( "Hello World!" );

Here is an output screenshot of a program I created with the
TextAreaOutputControl:

http://www.geocities.com/buddybob23/ConsoleIntercept.jpg

Hope it helps!

Best Regards,
Ranganath Kini


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.