System.out.println output to JTextArea
Question
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.
Solution
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.
OTHER TIPS
Well You can do it using the jTextArea.append("Your String")
method
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
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