Pedro
Pedro

Reputation: 11854

System.out.println output to JTextArea

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.

Upvotes: 3

Views: 4484

Answers (4)

Ramindu Samarawickrama
Ramindu Samarawickrama

Reputation: 313

Well You can do it using the jTextArea.append("Your String") method

Upvotes: 1

Aaron Digulla
Aaron Digulla

Reputation: 328556

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.

Upvotes: 11

peter.murray.rust
peter.murray.rust

Reputation: 38033

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

Upvotes: 0

Malaxeur
Malaxeur

Reputation: 6043

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

Upvotes: -1

Related Questions