Search code examples
javaswingjdialog

JDialog setVisible(false) vs dispose()


Does it make sense to use setVisible(false) on a dialog and reuse it later or is safer to call dispose() every time and to make a new JDialog. What about memory leaks with setVisible(false)?

EDIT: My Question isn't so much about quitting the application. More about Dialogs that have the main frame as parent and are opened and closed during the application life time. E.g. let's say my applications has about 10 dialogs that display different data every time I open them. Should I reuse the instances and use setVisible() or should I make a new Dialog every time and dispose() them on closing.


Solution

  • I would recommend using dispose() to release resources and free up memory. If you want to show the dialog again, simply invoke setVisible(true).


    It's important to note that when the last displayable window within the Java virtual machine (VM) is disposed of, the VM may terminate. See AWT Threading Issues for more information.