Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

output to JTextArea

 
ryan headley
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic