How to properly hide a JFrame
Asked Answered
F

4

7

I have a very simple JFrame window that contains one button: No.

In the main function I set setVisible(true); my JFrame and in the No button listener I want to close the window so I set the visibility to false: setVisible(false); and after that I do System.exit(0); in order to prevent possible memory leaks when running the program many times.

I have two questions:

  1. Do I really need to System.exit(0); in the above case?
  2. If I have this JFrame as a popup window, I can't really use System.exit(0); because this will terminate the whole program. So how can I properly close the popup window and stay in the main JFrame window? (Now I close it only by setVisible(false); and when I do it several times through the program execution, the program turns very slow).
Fastening answered 3/1, 2013 at 11:58 Comment(1)
you can use the function dispose()Eldwin
W
9
  1. use CardLayout

  2. if is there real reason for another popup container

  3. put both together, above two points, to use CardLayout for popup JDialog with parent to JFrame, notice after switch from one card to another could be / is required to call JDialog.pack()

Warwick answered 3/1, 2013 at 12:2 Comment(1)
@MarounMaroun see here The Use of Multiple JFrames, Good/Bad Practice? and +1 mKorbel for cardlayout. See here for an exampleAtrophied
L
4
  1. setVisible will cause slowdown
  2. dispose will cause slowdown
  3. System.exit will close entire JVM

Therefore, you should reuse a single JFrame or JDialog.

In the button's ActionListener, invoke frame.setVisible(false);. Then instead of creating a new frame just do frame.setVisible(true);. If you want to change the contents of the frame, there is the function frame.getContentPane().removeAll();.

Leonhard answered 3/1, 2013 at 12:3 Comment(1)
hmmm even I upvote, now I see...., please to change frame.removeAll(); to frame.getContentPane().removeAll(); I saw a few time that you'll able to remove RootPane then there stays only translucent window with Toolbar with Borders, any contents, nothing elseWarwick
M
3

Just add this: JFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE). Note: The default option for JFrame is HIDE_ON_CLOSE.

Misology answered 3/1, 2013 at 12:3 Comment(4)
This will cause the program to slow down much like setVisible(false);.Leonhard
@Doorknob According to JFrame's doc( docs.oracle.com/javase/6/docs/api/javax/swing/… ), DISPOSE_ON_CLOSE: Automatically hide and dispose the frame after invoking any registered WindowListener objects, which is different from HIDE_ON_CLOSEMisology
Dispose does not mean "free up resources." Try making a program that makes tons of JFrames and disposes them, it will slow down a LOT.Leonhard
@Doorknob "Try making a program that makes tons of JFrames" What number is 'tons'? While I am not a huge fan of multiple frames, you would not notice a slow down from 100 frame instances (until you go to close them and realize that you need to close each of the 100!).Debbydebee
A
0

You can use the dispose() method of the JFrame class to close the frame and release all resources associated with it, including its child components.

Abscond answered 13/2, 2023 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.