Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redirecting System.out to a JTextArea

Status
Not open for further replies.

mlundin617

Technical User
Feb 16, 2001
4
0
0
US
I've got a couple nicely working classes that throw their print with System.out.println. My question is, is there a way to redirect the System.out.println so that these lines print to a JTextArea?

I basically want my swing front-end to print out what's going on in the background. Is this possible?

Thanks for any assistance,

Michael Lundin
 
Michael,

You'll want to use readLine() to display the text in the JTextArea.
Code:
JTextArea txaMessages = new JTextArea;
BufferedReader in;

and then something like this:

try {
  while(true) {
     txaMessages.append(in.readLine() + "\n");
  }
}

That should work.

Mark Banker
 
Thanks Mark,

But I'm still confused. How should I set up that BufferedReader? The compiler spits it out when I try it just like that, since in isn't initialized.

Also, where should I put the while loop, since it looks like that's going to be running all the time?

Thanks again for your help, and I'm sorry if this is a dense question,

Michael
 
"in" won't necessarily have to be from a BufferedReader. I showed it that way because of the use that I've made with it recently, displaying a message that was received through a client/server chat.

You can use the name of the variable that you want to output in its place. Where is the text that you want to display coming from?

The try and while should be placed inside a method (maybe private void displayText(), inside the class that is your client GUI. How you get access to the text that you're displaying depends on the class modifiers used in the class that is generating the text.

I hope this helps, Michael. Ask again if you need more help.

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top