selsted
asked on
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(my Panel);
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!
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(my
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!
You can always add a System.exit(0); at the end.
If you are using JDK1.4 you could also do that as well:
myDialog.setDefaultCloseOp eration(JD ialog.EXIT _ON_CLOSE) ;
myDialog.setDefaultCloseOp
ASKER
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.
So System.exit is not an option.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.