Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

JDialog dispose question

Posted on 2003-03-24
5
Medium Priority
?
1,926 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
  • 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month15 days, 13 hours left to enroll

580 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