Hello. I am making an application, and at several times the user will
need to pick an object from a list of objects. To do this, I have a
class extending JFrame that displays a JComboBox and has a button "ok"
for when the user picks the one he/she wants. But, what happens next
depends on what the user picks, so I need to wait until the user clicks
"ok" in the other JFrame.
I can't think of a way for the external JFrame to call the main one
that works. The object chosen could go one of several places, so the
easiest way is just having a method in the main JFrame that returns
whatever the user picks. However, I can't just stop in the middle of
the method and wait until "ok" is clicked. So I set it up a while loop
that calls a method in the external JFrame and checks if the button has
been clicked. It keeps going thru the loop until the button is
pressed, then retrieves the object that the user chose.
A problem with that setup is the while loop takes up all the time, and
the JFrame where the user chooses the object isn't even fully drawn.
Now, a java class, JOptionPane, does what I need to do. I was taught
to use this right when I was learning to prompt basic info from the
user. It DOES wait until info has been entered and "ok" has been
clicked. I don't know how it works though, but it shows that it can be
done.
I tried doing "Thread.currentThread().sleep(100)" but it still does
the same thing, the other JFrame isn't even drawn. I also tried
"this.wait(100)", but says that "this" is not a proper owner or
something like that.
I really need to get something like this to work - having the other
JFrame call the main one and tell it what to do will be very hard to do
in my case. Any help will be very appreciated.
Bill Tschumy - 04 Apr 2005 02:00 GMT
> Hello. I am making an application, and at several times the user will
> need to pick an object from a list of objects. To do this, I have a
> class extending JFrame that displays a JComboBox and has a button "ok"
> for when the user picks the one he/she wants. But, what happens next
> depends on what the user picks, so I need to wait until the user clicks
> "ok" in the other JFrame.
Normally you would use a modal JDialoog rather than a JFrame for this. Is
there some reason the standard approach won't work?

Signature
Bill Tschumy
Otherwise -- Austin, TX
http://www.otherwise.com
Kari Ikonen - 04 Apr 2005 18:35 GMT
> class extending JFrame that displays a JComboBox and has a button "ok"
> for when the user picks the one he/she wants. But, what happens next
> depends on what the user picks, so I need to wait until the user clicks
> "ok" in the other JFrame.
You could pass in approriate callback interface to JFrame when
creating/showing it. OK -action, will then call this interface.
E.g.
public interface CommitChanges {
void commitChanges(Object pData);
}

Signature
KI
Thomas Weidenfeller - 05 Apr 2005 07:49 GMT
> Hello. I am making an application, and at several times the user will
> need to pick an object from a list of objects. To do this, I have a
> class extending JFrame that displays a JComboBox and has a button "ok"
> for when the user picks the one he/she wants. But, what happens next
> depends on what the user picks, so I need to wait until the user clicks
> "ok" in the other JFrame.
What's this recent JFrame craze? Is there a new book out, only teaching
JFrame?
A similar issue has been discussed just a few days ago so you might want
to have a look at
http://groups.google.com/groups?selm=d2gvli%24jvu%241%40newstree.wise.edt.ericsson.se
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
John McGrath - 06 Apr 2005 02:13 GMT
> Hello. I am making an application, and at several times the user will
> need to pick an object from a list of objects. To do this, I have a
> class extending JFrame that displays a JComboBox and has a button "ok"
> for when the user picks the one he/she wants. But, what happens next
> depends on what the user picks, so I need to wait until the user clicks
> "ok" in the other JFrame.
In addition to what the others have said: You are trying to use a JFrame
for exactly the purpose that a modal dialog is intended. What you are
doing is somewhat akin to building an flying machine out of a lawn mower,
rather than use an already built airplane. You can build a dialog out
of a JFrame, just as you can build a flying machine out of a lawn mower
(http://gprime.net/video.php/flyinglawnmower), but unless you are in it
for the adventure of doing something that few others have done (or would
want to do), I would go with the more standard approach.
> So I set it up a while loop that calls a method in the external JFrame
> and checks if the button has been clicked. It keeps going thru the
> loop until the button is pressed, then retrieves the object that the
> user chose. A problem with that setup is the while loop takes up all
> the time, and the JFrame where the user chooses the object isn't even
> fully drawn.
It sounds like you are doing this in the event handler for a button or
something similar. Event handlers heed to do a small amount of work and
then quickly return. Since both event handlers and component painting
execute in the event dispatch thread, nothing will paint until the event
handler returns.
> Now, a java class, JOptionPane, does what I need to do. I was taught
> to use this right when I was learning to prompt basic info from the
> user. It DOES wait until info has been entered and "ok" has been
> clicked. I don't know how it works though, but it shows that it can be
> done.
Actually, a JOptionPane is a component that arranges a message (which can
be many different things, including arbitrary components), along with an
optional Icon and some standard buttons. It is not actually a top-level
window. But the JOptionPane class contains convenience methods to create
a JOptionPane and show it in a modal JDialog.
So you were taught to use a modal dialog - you were taught correctly. If
the dialog does not require error processing and validation, JOptionPane
is the way to go. With more complex requirements, you probably want to
create the JDialog yourself, since this affords you greater control.

Signature
Regards,
John McGrath