?
Solved

JDialog dispose question

Posted on 2003-03-24
5
Medium Priority
?
1,913 Views
Last Modified: 2007-12-19
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!
0
Comment
Question by:selsted
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 8194613
 You can always add a System.exit(0); at the end.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8194638
 If you are using JDK1.4 you could also do that as well:

myDialog.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
0
 

Author Comment

by:selsted
ID: 8194646
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
 
LVL 2

Accepted Solution

by:
glottis earned 400 total points
ID: 8194797
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
 

Author Comment

by:selsted
ID: 8195067
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month13 days, 5 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question