• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

output to JTextArea

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic