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.
setVisible(false) doesn't free the resources used by the JFrame--it just takes it off the screen.
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.
The JDialog class is a subclass of the AWT java. awt. Dialog class. It adds a root pane container and support for a default close operation to the Dialog object . These are the same features that JFrame has, and using JDialog directly is very similar to using JFrame .
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.
I still can't see any diference between JDialog#dispose();
and JDialog.setVisible(false)
more here, each of them could be wakeup for reuse, and doesn't matter if was/were Disposed or Visibled
my view is that this question must be splited to three separates areas
1) some JFrame
is parent for JDialog
or JWindow
(exist only is is there JFrame
), then last one must to turn-off the lights
2) without parent for JDialog
3) there still exist another JFrame, JDialog or JWindow
, then last one must to turn-off the lights
Window[] wins = Window.getWindows();
System.exit(0);
JFrame
with JFrame.EXIT_ON_CLOSE
, or another way could be implements WindowsListener
with System.exit(0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With