waiting until JFrame is closed

D

deej21

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 pops up a small JFrame, and waits 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.
 
A

amitdev

From your explanation, things are not very clear why you want to do it
this way.
In any case you can achieve what you want by using wait() and notify().
In your main JFrame, have that method (where you have the while loop)
synchronized on something and do a wait().
Now on the other frame, when OK is pressed do a notify(). This should
also be synchronized with the same object.

aMit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top