JDialog setVisible(false) vs dispose()

僤鯓⒐⒋嵵緔 提交于 2019-11-27 14:19:40

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.

mKorbel

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);

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!