aspose file tools*
The moose likes I/O and Streams and the fly likes output to JTextArea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "output to JTextArea" Watch "output to JTextArea" New topic
Author

output to JTextArea

ryan headley
Ranch Hand

Joined: Jun 28, 2000
Posts: 156
Okay, got a GUI program that runs a ping on a selected machine to see if its alive. I would like to have the responses show up in the JTextArea that I have. How can I get that done? If I run it now, I can't even see the command run in the DOS window, but I think that's because when you use:
Runtime.getRuntime().exec("...the command stuff here...");
the output defaults to stderr. (I think I remember reading that somewhere...)
If that is the case, how do I change where the output shows up? I know I can port the output to a file and then read it in, but shouldn't there be a way to write it directly to the window??
Ryan
P.S. Just upgraded to Ranch Hand --- YeeeHaw!! 8)


Ryan Headley<br /><a href="http://www.sudovi.com" target="_blank" rel="nofollow">http://www.sudovi.com</a>
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
The program you are calling is what determines which stream it is writing to. Most applications write to stdout. A few (and javac is one of them) write to the err stream. Ping does not on win32. To do this, you create an input stream off the process that Runtime.exec returns and read the response.
Example :

Hope This Helps


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Carl, you are the freakin' Wyatt Earp of the JavaRanch! That is the coolest thing I have seen in a month! I used your code fragment to execute cmd on my NT machine. Then I added a line to capture the output stream for the process and sent "dir *.java" to it. Sure enough, I saw the listing in my application's output window. Now I can do anything I want to in DOS and my java application sits on top of the process and monitors the I/O. I am LOVING this!
[This message has been edited by Nick Riviera (edited November 13, 2000).]
ryan headley
Ranch Hand

Joined: Jun 28, 2000
Posts: 156
WOW!! That's pretty sweet. I obviously need to make my GUI multi-threaded, but that's another ball game. I just wanna get the basics down first.
I must ask however, why do you put the commands into an array of Strings?? Will I need to do that?? What I need to do is select the machine name from a list and then feed it to ping. So could just do a:
String s = t1.getSelectedText();
And then feed s into that array?? (assuming t1 is my JTextField).
Also, is there anyway to update the JTextArea as is happens, (no big deal just curious). What happens currently is the command runs, then places the output on the textarea. Is there anyway to make it "real time" so that you see the output happening??
Thanx again!
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
See: http://java.sun.com/j2se/1.3/docs/api/java/lang/Runtime.html
The first parameter in the array is the command and the remaining parameters are the arguments.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Originally posted by ryan headley:

Also, is there anyway to update the JTextArea as is happens, (no big deal just curious). What happens currently is the command runs, then places the output on the textarea. Is there anyway to make it "real time" so that you see the output happening??

You can create your own object that extends textarea and implements observer. There is a good discussion of this and examples at http://developer.java.sun.com/developer/technicalArticles/Programming/KeepObjectsInSync/index.html

------------------
Hope This Helps
Carl Trusiak
Muhammad Ali
Greenhorn

Joined: Dec 04, 2000
Posts: 28
BufferedReader reader = new BufferedReader(new InputStreamReader(new DataInputStream(new BufferedInputStream(p.getInputStream()))));
Why do we need so many classes / wrappers to read the input? Can someone explain the need for each of them (one by one)?
Thanks...
Muhammad Ali Shah
Karachi, Pakistan.
Pintea Haiducu
Greenhorn

Joined: Jan 06, 2010
Posts: 2
You can use append method of JTextArea.
If the output is flushed only after your action executes, it means that you must put your action in a separate thread.
 
 
subject: output to JTextArea
 
Similar Threads
how do i pass some time?
getting output from DOS into GUI
Can I fill a text area with System.out?
Creating a command prompt is swing
redirect system.out ??