JDialog dispose question

I have the following code:

public static void main(String[] args)
{
  JDialog myDialog = new JDialog((JFrame)null, true);
  myDialog.show();
  myDialog.dispose();
  System.out.println("Out");
}

In the first line I make sure that the dialog is modal, this is to make sure dispose is not called before the dialog is hidden.
Then I show the dialog,
and then I dispose it.
Lastly I make a println.

What I would expect:
I would expect a dialog to appear on the screen, with the possibility to close it on the close button of the window (upper right corner in windows).
After I close the dialog, I would expect to see the message "Out" printed,
and lastly the program to terminate.

What is wrong:
The program does print "Out" after closing the dialog, but never terminates.

What am I missing here?


Slight modification I have tried, with the same result:
Before the show, I add:
  myDialog.setContentPane(myPanel);
myPanel has a button, and upon clicking it, the dialog is hidden, "Out" is printed, but the program doesn't terminate:
private void clickButtonQuit()
{
  Component comp = this;
  while (comp!=null && !(comp instanceof JDialog)) comp = comp.getParent();

  if (comp==null) return;

  ((JDialog)comp).dispose();
}

Thanks!
selstedAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
 You can always add a System.exit(0); at the end.
0
girionisCommented:
 If you are using JDK1.4 you could also do that as well:

myDialog.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
0
selstedAuthor Commented:
I should have mentioned that I show dialogs within my application, and this is a potential memory leak I want to fix.
So System.exit is not an option.
0
glottisCommented:
when you show any type of JComponent a new thread is started.
JDialog myDialog = new JDialog((JFrame)null, true);

you are saying that myDialog has a parent null of type JFrame. so jvm starts a new thread for null frame (i think)

so therefore you have to type System.exit(0) after it prints out.

OR

change it to meet girionis way:

public static void main(String[] args) {
 JDialog myDialog = new JDialog((JFrame)null, true);
 myDialog.setDefaultCloseOperation(3);
 myDialog.show();
 myDialog.dispose();
 System.out.println("Out");
}

the setDefaultCloseOperation method exists in jdk1.2.2 and above, only the constant EXIT_ON_CLOSE is in jdk1.3 and above.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
selstedAuthor Commented:
Setting the close operation to EXIT_ON_CLOSE in 1.4.1 doesn't do anything, and according to the api, only DO_NOTHING, HIDE, and DISPOSE is possible to set.

After making some tests, I think I have figures out what happens in your (I think) comment.

Starting a program, counting the thread, and exiting gave a result of 4 threads. However counting the thread in my original problem gave a result of 6 threads. These 2 extra threads is surely the cause of why it is not terminating. My concern was more or less the memory leak (though other irrelevant problems occure from this problem). Making a loop where I created, showed, and disposed JDialogs as in the problem, I monitored the memory usage, and the number of threads. The memory did as it always does, increasing and decreasing, but the thread count stayed at 6.

Now I just have to figure out if other windows are present, so that I can make a System.exit if there isn't.

I accept your comment as the answer.

Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.