views:

400

answers:

3

Hello,

I would like to everytime I call System.out.println to append to a given JTextArea, without having to change all calls to System.out.println... Is this possible?

Thank you.

A: 

I suppose you could potentially use some form of AspectJ to do it, but I think that may be overkill. What I would do though is create a method that would both print and append.

public void printAndAppend(String text) {
      System.out.println(text);
      textArea.append(text);
}

You can then just do a global find and replace for System.out.println and replace it with printAndAppend

Malaxeur
that would work, but I don't want to touch all this legacy stuff..
Pedro
A: 

I don't think there is a simple way. I generally try to avoid System.out calls in my code for exactly this sort of reason. If you have a method like (say) MyUtil.myOutput() then you can make a single change and reroute it where you want

peter.murray.rust
yeah, it's more for dealing with some legacy stuff I have here.. sure if I would make it from stratch I would use log4j
Pedro
+9  A: 

Versions of Java since 1.5 have System.setOut() which allow you to install your own PrintStream. Just create a simple OutputStream which appends the data it get through write() Then wrap it in a PrintStream and install it.

Aaron Digulla
+1, beat me to it
ammoQ
thanks, that's it :).
Pedro