Redirecting System.out.println()

I

Ike

Is anyone familiar with a way to redirect System.out.println()? I'd like to
redirect it to a JTextArea if possible. Thanks, Ike
 
R

Roedy Green

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.
 
D

Daniel Dyer

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.
 
I

Ian Mills

Ike said:
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
 
F

Frank D. Greco

Ian Mills said:
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.
 
F

Frank D. Greco

Roedy Green said:
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|
+==========================================+
 
I

Ike

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
 
S

steve

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

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 shit 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);
}
*/
}
 
T

Thomas Fritsch

Daniel Dyer said:
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
 
R

Ranganath Kini

Ike said:
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
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top