aspose file tools*
The moose likes Java in General and the fly likes I have a problem creating this object multiple times (thanks to some Listener) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "I have a problem creating this object multiple times (thanks to some Listener)" Watch "I have a problem creating this object multiple times (thanks to some Listener)" New topic
Author

I have a problem creating this object multiple times (thanks to some Listener)

Clark Johnson
Greenhorn

Joined: Mar 08, 2007
Posts: 20
I am trying to create an object multiple times, using a do-while loop, but the loop's test is executed before the object I have created has finished doing what it is supposed to be doing. I can see where this is failing. After the object's constructor is called, an instance of that object is created and the loop's test is performed. I want the loop's test to be performed after the user has entered some input into that object, not after the constructor method returns.

The following code illustrates my problem. This is, by the way, just a simplistic example that captures the part I am having trouble with. The program I am working on is more complicated. It should be noted that I can't edit the object related methods of the "Program" class; this is a problem that must be addressed from within the main method.

If anyone can shed some light into this I'd appreciate it.

Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
The call to showDialog is hidden inside the buttons ActionListener.
So you have to find a way to fire an ActionEvent for the button.
Testing for String equality works better with the equals method.
Clark Johnson
Greenhorn

Joined: Mar 08, 2007
Posts: 20
"Testing for String equality works better with the equals method."

I know, but that's not my problem.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think what you're running into is the AWT / Swing event thread. Creating a window (JFrame) creates a new thread where button events and such take place. Your main thread does not wait for that one but goes merrily on its way.

You may need to make the main thread wait() on some object and do a notifyAll() on that object after reading the text. Any object will do. Maybe pass a String along to the program constructor and make sure it's available to the method that prompts for text.

I'm hedging my bets here because I don't do much Swing these days. Did that make sense? Check it out by displaying the thread name in main and in the event handler.
[ March 17, 2007: Message edited by: Stan James ]

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I have a problem creating this object multiple times (thanks to some Listener)