File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JDialog.setVisible(false) and JDialog.dispose() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JDialog.setVisible(false) and JDialog.dispose()" Watch "JDialog.setVisible(false) and JDialog.dispose()" New topic
Author

JDialog.setVisible(false) and JDialog.dispose()

Ted Smyth
Ranch Hand

Joined: May 28, 2008
Posts: 73
Good Afternoon all,

I had a question the answer to which I couldn't really infer, so I thought I would pose it to the ranch:

Suppose one creates a custom non-modal JDialog. There's some kind of closing button that handles hiding the window. In the corresponding handler (actionPerformed() or what-have-you), setVisible(false) is called to hide the window.

What is the difference between:

A: References to the dialog are kept, and dispose() is called after setVisible(false);

B: NO references to the dialog are kept, and dispose() is never called.


I guess what I'm really trying to ask is: what is the point of dispose()? Should I be explicitly disposing dialogs, or let the GC deal with them? Javadoc tells me that it releases native resources associated with the dialog, but that they can be rebuilt via pack().

Regards,


Edward Smith
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
There's some kind of closing button that handles hiding the window.
With the setDefaultCloseOperation method you can specify whether to hide or dispose the dialog.

what is the point of dispose()?
releases native resources associated with the dialog
As you said, reclaim unused resources by removing the native peer.

Should I be explicitly disposing dialogs, or let the GC deal with them?
Seems to be a matter of personal opinion/preference.
If you're finished with the dialog or intend to reuse it for something else calling dispose on it may make sense.
The dialog and its children will not be GC'd unless all references to it/them are made null, ie, until there is no way that the app could ever refer to it/them again. Associated listeners may persist after this and would have to be removed beforehand, if desired.
Ted Smyth
Ranch Hand

Joined: May 28, 2008
Posts: 73
With the [i]setDefaultCloseOperation method you can specify whether to hide or dispose the dialog.[/i]
Well yes, but that's not really within the scope of my question.

Seems to be a matter of personal opinion/preference.
If you're finished with the dialog or intend to reuse it for something else calling dispose on it may make sense.

So then keeping Java references to dialogs while they are being hidden (for example, keeping a few pre-built dialogs around to show periodically), but disposing of native resources (for other processes, apps, etc running on the OS) is a memory efficient approach?
 
wood burning stoves
 
subject: JDialog.setVisible(false) and JDialog.dispose()