Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Redirecting output to jtextarea  RSS feed

 
Ashwani Singh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a swing based application,i want to see the output(every System.out.println()) on the jtextarea component of my GUI rather than on general console.I tried something like this:

public class CustomOutputStream extends OutputStream {
JTextArea jtextarea;
CustomOutputStream(JTextArea j)
{
jtextarea=j;
}
@Override
public void write(int b) throws IOException {
// throw new UnsupportedOperationException("Not supported yet.");

jtextarea.append(String.valueOf((char)b));
jtextarea.setCaretPosition(jtextarea.getDocument().getLength());
}
}



and in my GUI creation class,i coded:

public Main_frame() {

initComponents();
System.setOut(new PrintStream(new CustomOutputStream(jTextArea1)));
System.setErr(new PrintStream(new CustomOutputStream(jTextArea1)));
}

where Main_frame() is my constructor of the gui building class.

Problem is that for some duration the textarea is showing the output,but after several events,the text area is not displaying anything.I suspect some sort of buffer needs to be flushed but when and where no idea.Please help.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not exactly sure what the problem is, but it looks like it could be some sort of race condition. Remember that Swing components are not thread safe. You need to push the append for the JTextArea into the Event Dispatch Thread. Lookup SwingUtilities#invokeLater() to do the job.

It might also be best if you fork the output - put it in your GUI but also log it to a file so you can see it later if something (like this) happens.
 
Ashwani Singh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please guide me where exactly i should put the

System.setOut(new PrintStream(new CustomOutputStream(jTextArea1)));
System.setErr(new PrintStream(new CustomOutputStream(jTextArea1)));
statements and write() method of OutputStream class as i also think that since the setOut and setErr methods are called in the constructor of the gui building class,i.e in the initial thread,this might be an issue.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out a similar discussion http://www.coderanch.com/t/344865/GUI/java/Input-Output-Streams-JTextArea

UI topics are best suited on the UI forums. Moving...
 
Rob Camick
Ranch Hand
Posts: 2753
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out Message Console for an approach that allows you to use different colors for "out" and "err" messages.
 
Ashwani Singh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like if suppose i move the System.setOut and System.setErr methods in any ActionPerformed() method of a button click,will this problem be resolved? Please respond.

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try it? What was the result you observed?
 
Ashwani Singh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i tried it,as i said earlier also,jtextarea was displaying the output for certain duration of time and after running the application for some time,as mentioned by Steve,probably due to race condition problem used to occur.For now i moved those codes to actionperformed method and i ran the application and it was fine until that duration of running and then i stopped the application.Now if later again some race condition occurs then i can't say.I am not sure wether problem is resolved or not.If you can please suggest me a sure shot way of testing it,it would be helpful.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please share your SSCCE (<=link; click to see what SSCCE really means) which we can download and run on our machines so we can see the problem in action.
Don't forget to UseCodeTags
 
Ashwani Singh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps its resolved,i will get back here if again any issue arises.Thank you.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwani Singh wrote:Can you please guide me where exactly i should put the

System.setOut(new PrintStream(new CustomOutputStream(jTextArea1)));
System.setErr(new PrintStream(new CustomOutputStream(jTextArea1)));
statements and write() method of OutputStream class as i also think that since the setOut and setErr methods are called in the constructor of the gui building class,i.e in the initial thread,this might be an issue.


Where you call these methods isn't important. Where the text gets added to the JTextArea is important - and it must be done in the EDT. Making sure those methods get called in the EDT doesn't resolve the fact that any thread can call System.out.println() and thus try to write to your JTextArea in the non-UI controlling thread.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!