aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTextArea listening for messages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextArea listening for messages" Watch "JTextArea listening for messages" New topic
Author

JTextArea listening for messages

Jason Richard
Ranch Hand

Joined: Oct 05, 2007
Posts: 69
Hi everybody,

I'm fairly new to swing at least on any sort of significant level, so if I don't explain something too well to start, I apologize in advance. I have a non-GUI based application that I developed, and taking the code that somebody wrote already using netbeans for an External GUI Launcher type program and I'm integrating it directly into my existing code. The plan is to eventually create a new GUI, but in order to have a better interaction between the user and the product without waiting for months of GUI development i figured I would start here.

So the GUI part of the program now is based off a SingleFrameApplication, and currently this SingleFrameApplication will fire off a JDialog box at a certain point. From this JDialog box, we used to kick off my program using

p = Runtime.getRuntime().exec("my_program.cmd");

we would then listen for output in a jtextarea by




This worked out ok for what it was. But now I want to be able to (from outside the gui code) directly send my logging to this text area (and eventually update other text areas and other swing objects).

At first I thought all i would have to do would be to access the component, but that is not working out so well. Then I thought I'd try to add a Listener to the textArea, but you can't do that.

So my code is all over the place, and before i follow up with actual code if needed, I was hoping somebody had run into something along these lines and can point me in a direction that makes sense to start.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18659
    
    8

Jason Richard wrote:At first I thought all i would have to do would be to access the component, but that is not working out so well.


That's what I would have thought too. So would you like to clarify "not working out so well"?

In the meantime I'm going to guess that you're running this log-producing code in the Swing thread. If that phrase doesn't ring a bell, check out the tutorial: Lesson: Concurrency in Swing.
Jason Richard
Ranch Hand

Joined: Oct 05, 2007
Posts: 69
Paul Clapham wrote:
Jason Richard wrote:At first I thought all i would have to do would be to access the component, but that is not working out so well.


That's what I would have thought too. So would you like to clarify "not working out so well"?

In the meantime I'm going to guess that you're running this log-producing code in the Swing thread. If that phrase doesn't ring a bell, check out the tutorial: Lesson: Concurrency in Swing.


So the log producing code is in the Swing thread but in a class outside of the GUI itself. If a button is pressed I'm now I'm just launching my old codes main

MyCode.main(args);

and the code kicks off great, and as of right now All my log files are created, and my console output is generated correctly. All i was trying to do was for starters was to duplicate the console output to the text area.

I will take a look at that link you provided thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18659
    
    8

Jason Richard wrote:So the log producing code is in the Swing thread but in a class outside of the GUI itself.


Here's a basic rule of Java: it doesn't make any difference what class a piece of code is located in. As long as the code can get a reference to an object, it can call that object's methods (if they are accessible to the code, that is).
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Then I thought I'd try to add a Listener to the textArea, but you can't do that.

try a DocumentListener
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JTextArea listening for messages