Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JFrame and setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE)

Tags:

java

swing

jframe

On javadoc, the HIDE_ON_CLOSE default option says that

Automatically hide the frame after invoking any registered WindowListener objects.

Now what it means "HIDE"? the realtive object is destroyed or just hide and continue using resources?

like image 693
giozh Avatar asked Aug 13 '26 07:08

giozh


1 Answers

On javadoc, the HIDE_ON_CLOSE default option says that

Automatically hide the frame after invoking any registered WindowListener objects.

Now what it means "HIDE"? the realtive object is destroyed or just hide and continue using resources?

  • HIDE_ON_CLOSE is the same as JFrame.setVisible(false),

  • then JFrame in only hiden, invisible, isn't destroyed somehow (the same for JFrame.dispose()), by JFrame.setVisible(true) is again visible on the sceen and without any changes

EDIT

@giozh wrote and if i want to destroy the jframe (without close the entire application)?

  1. by default there isn't any reason, because by default there no reason to create another JFrame, don't do that, use CardLayout (with JFrame.pack() if is neccessary to change JFrames size on the screen)

  2. and all those Object stays and increasing JVM memory, never will be CG'ed, then there isn't siginficant difference for JVM memory between JFrame.HIDE_ON_CLOSE, JFrame.DISPOSE_ON_CLOSE or JFrame.setVisible(false)

  3. (in the casse that you hate CardLayout) you can to remove all JComponents from JFrames ContentPane, then to add new JComponents, set LayoutManager and last code lines (after all changes to the already visible JFrame is done) would be JFrame.(re)validate();, JFrame.repaint(); and JFrame.pack();

like image 122
mKorbel Avatar answered Aug 14 '26 22:08

mKorbel