JDialog setVisible(false) vs dispose()
Asked Answered
H

4

23

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.

Henshaw answered 31/8, 2011 at 11:58 Comment(2)
dispose() will reuse those same instances, while releasing resources and freeing up memory.Antipyrine
Also nice to know: Once a JDialog was pack()ed or setVisible()d, it won't be garbage collected until you call dispose()!Henshaw
A
30

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.

Antipyrine answered 31/8, 2011 at 12:4 Comment(2)
JWindow & JDialog missed method finalize(), then isn't so easy, nor without GC ..... btw +1 #6309907Catamnesis
Maybe you need to mention that if you reuse the same dialog a lot, is better to use setVisible(false) so JVM don't need to allocate the resources each time you want to display it.Ansilma
C
2

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

  • reachable by using --> Window[] wins = Window.getWindows();
  • last one must to turn-off the lights --> System.exit(0);
  • I suggest that there in all of possible cases must exist visible JFrame with JFrame.EXIT_ON_CLOSE, or another way could be implements WindowsListener with System.exit(0);
Catamnesis answered 31/8, 2011 at 13:12 Comment(0)
G
0

Calling dispose() frees the resources associated with the dialog. You can keep the dialog around after you dispose() it. If you are worried about having too many dialogs laying around, use a WeakReference to hold the reference. That will ensure that your dialogs you are not using only survive garbage collection as long as the space they occupy is not needed.

Gasperoni answered 20/11, 2013 at 20:50 Comment(0)
B
0

I experienced a difference when a window shall be hiden twice (in cause of bad software design e.g.) If you dispose an already disposed window the VM hangs. (java 8) If you setvisible false on an already invisible window life goes on...

Barn answered 15/4, 2021 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.