It's not a secret anymore!*
The moose likes Java in General and the fly likes Real-time Printing on other application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Real-time Printing on other application" Watch "Real-time Printing on other application" New topic
Author

Real-time Printing on other application

Rabih Tayyem
Greenhorn

Joined: Sep 28, 2004
Posts: 10
Hi,
I have a GUI Form that is calling many other programs statically

ex: Class1.main (args);
Class2.main (args);

my Form has a TextArea (say TA)

What I am trying to do is to let my called programs (Class1, Class2) print on TA (of the calling program) in Real-Time (Whenever I issue a print command in Class1, it will directly be printed on TA).

is that doable???
Any help would be much appreciated..

Regards,
Rabih G. Tayyem


Rabih G. Tayyem
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You could pass your textarea (a name starting with lower case would be good) as one of the parameters to each subprogram.

Sub1.main(myTextArea, other, args);

Then Sub1 would have a reference to something it could write on. This works but is kinda bad object design because it exposes the workings of your main program to each subprogram, gives subprograms the ability to mess up the textarea, makes subprograms work with text areas only when you might want them to use some other output some day, and might have threading issues. Whew!

I'd rather give the subprograms an argument of some object that is interested in what they have to say. This is often called a listener. Let's say our listener object has a method called print.

Sub1.main(myListener, other, args);

Sub1 could have lines like this:

aListener.print("A message");

Now the listener can manage adding "A message" to the text area and solve many of the problems I listed above - more detail if you want it later.

Does this make sense?

I took a guess at the problem you're trying to solve. You might have been asking to change the system.out.println() of the subprogram to do write to the text area. We can kinda do that, too. Lemme know if I read this wrong.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Rabih Tayyem
Greenhorn

Joined: Sep 28, 2004
Posts: 10
Dear Stan,

Thanks for your reply and Sorry it took me a long time to reply.

First,...yes you "discovered" my problem. my subprograms (5 subprograms) have system.out.println(..) to log the action on the screen (they're doing huge calculations that need to be logged). Now, I need to show this printing on the text area of my GUI Form.

Second, Regarding your reply... you told me to use a listener...
Sub1.main(myListener, other, args);

I did not get that point....what is mylistener??? and what is alistener??
it does make sense when you think of a listener checking (listening) for a message and delevering it when recieved...but How to do that??

A direct question would be : what is the type of mylistener and alistener?

Thanks again for your help.

Regards.
Rabih Tayyem
Greenhorn

Joined: Sep 28, 2004
Posts: 10
Sorry I forgot to tell you....

your first solution will print on the textarea but not in Real-time.

Threading problem: text will be shown only AFTER the main program finished executing...

Thanks again.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Rabih Tayyem:

Threading problem: text will be shown only AFTER the main program finished executing...


... only if you're invoking the main() routines on the Swing event thread, which you shouldn't do in the first place. If you're doing that, there's no way to make anything show up in the GUI until the called methods are done. Use a separate worker thread!

Now, to answer your question another way: look at the setOut() method of the System class. Call setOut() with a PrintStream wrapped around your own implementation of OutputStream which takes all the written bytes and appends them to the (J)TextArea. System.out.println() then automatically goes to the TextArea. Be sure to save the original value so you can restore it later! This is the solution most similar applications use.


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Excellent, thanks for jumping in. To get a handle on that Swing thread thing, look for a method called invokeLater() somehow related to Swing. It's a good excuse to explore the "Index" link at the top of the JavaDoc.
Rabih Tayyem
Greenhorn

Joined: Sep 28, 2004
Posts: 10
so far I did the following:

I wrote my own OutputStream (As suggested):


and before calling my programs, I diverted my system.out to the text area



Doing this made all my System.out.println(...) to go to the text area.

What left to be solved (and THIS is the big problem) is to deal with threading since nothing is showing on the text area until my original program finishes executing.

Please take into consideration that I have many programs called in sequence
ex:


All classes need to write on the textarea, but class2 is not supposed to run until class1 is 100% done. Same goes for class3.

can someone help me more on this Thread issue because I am lost.
Where am I supposed to have a thread ?
or in other words Which Thread I have to "invoke later" .

Ernest you said
This is the solution most similar applications use.
so do you have a sample code???

Thanks in advance.
Rabih Tayyem
Greenhorn

Joined: Sep 28, 2004
Posts: 10
in my second please ignore


Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

You'e got the System.out stuff just fine, so you don't need a sample there. But the other thing I said is that your

a.main()
b.main()
c.main()

is probably inside an event handler, and because of this, the GUI can't update at all until all three of these routines run. Instead, move these into a separate Thread, invoked from the event handler -- i.e.,



This will free up the GUI event thread to repaint the screen and such. Now, you might not want events handled until all of these codes run; to do that you need to fiddle with the "glass pane" of your JFrame. At that point -- and perhaps even at this point -- this thread ought to move to the Swing/AWT forum.
Rabih Tayyem
Greenhorn

Joined: Sep 28, 2004
Posts: 10
THANKS...

this one works...

Thanks all for your help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Real-time Printing on other application