• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic