Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / July 2005

Tip: Looking for answers? Try searching our database.

PrintWriter output to GUI

Thread view: 
C P - 26 Jul 2005 20:54 GMT
Hello
Is it possible to re-engineer an existing app that now statically creates a
PrintWriter and  outputs System.out to it with output_.println() to go to a
scrollable GUI component?  I believe not b/c no Swing app would have the
println method and I dont see how you to force it; however I am naive one
asking the question.  Looking for insight and a code snippet.  tia.
Thomas Fritsch - 26 Jul 2005 21:29 GMT
"C P via JavaKB.com" <forum@JavaKB.com> wrote.

> Hello
> Is it possible to re-engineer an existing app that now statically creates
[quoted text clipped - 4 lines]
> println method and I dont see how you to force it; however I am naive one
> asking the question.  Looking for insight and a code snippet.  tia.

It is actually quite simple. The key is the
 System.setOut(PrintStream)
method, and give it a PrintStream, which writes into a GUI component.
Try <http://www.google.de/search?q=System.setOut+%2Bswing>
to find some implementations of this approach.

Signature

"TFritsch$t-online:de".replace(':','.').replace('$','@')

C P - 26 Jul 2005 21:44 GMT
Hi Thomas
Thanks.  That really help with the "zen" of it all also.

>"C P via JavaKB.com" <forum@JavaKB.com> wrote.
>
[quoted text clipped - 9 lines]
>Try <http://www.google.de/search?q=System.setOut+%2Bswing>
>to find some implementations of this approach.
Rogan Dawes - 29 Jul 2005 14:28 GMT
> Hi Thomas
> Thanks.  That really help with the "zen" of it all also.
[quoted text clipped - 14 lines]
>>Try <http://www.google.de/search?q=System.setOut+%2Bswing>
>>to find some implementations of this approach.

Try this approach:

Create a "DocumentOutputStream" class that implements OutputStream, and
writes everything to an encapsulated Document.

Then you can call "getDocument()" on your class, and
textField.setDocument() to link it to the GUI class.

It's about 20 lines of code, in all.

You can also vary the type of document to control how the text is
presented. e.g. using a StyledDocument, as each line is written to the
stream, you can highlight alternate lines, etc.

Rogan
Rogan Dawes - 29 Jul 2005 16:34 GMT
> Hi Thomas
> Thanks.  That really help with the "zen" of it all also.
[quoted text clipped - 14 lines]
>>Try <http://www.google.de/search?q=System.setOut+%2Bswing>
>>to find some implementations of this approach.

Here is a link to an implementation. It is REALLY simple, as mentioned
in my other post.

Without the size-limiting, it probably WOULD be about 30 lines in total.

<http://cvs.sourceforge.net/viewcvs.py/owasp/webscarab/src/org/owasp/webscarab/ut
il/swing/DocumentOutputStream.java?rev=1.3&view=markup
>

Then just wrap this with a PrintStream, and you are in business.

i.e.

DocumentOutputStream dos = new DocumentOutputStream();
Document document = dos.getDocument();
textArea.setDocument(document);
System.setErr(new PrintStream(dos));

Note that Document may be updated in non-Event-dispatching threads. From
the JavaDoc for AbstractDocument (from which PlainDocument is derived):

Any models subclassed from this class and used in conjunction with a
text component that has a look and feel implementation that is derived
from BasicTextUI may be safely updated asynchronously, because all
access to the View hierarchy is serialized by BasicTextUI if the
document is of type AbstractDocument. The locking assumes that an
independant thread will access the View hierarchy only from the
DocumentListener methods, and that there will be only one event thread
active at a time.

Regards,

Rogan


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.