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).
Maroun
  • 94,125
  • 30
  • 188
  • 241

4 Answers4

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

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 3
    @MarounMaroun see here [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice) and +1 mKorbel for cardlayout. See [here](http://stackoverflow.com/questions/14011397/how-to-add-jpanel-by-clicking-jbutton/14012757#14012757) for an example – David Kroukamp Jan 03 '13 at 12:25
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();.

tckmn
  • 57,719
  • 27
  • 114
  • 156
  • 2
    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 else – mKorbel Jan 03 '13 at 12:19
3

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

Hui Zheng
  • 10,084
  • 2
  • 35
  • 40
  • 1
    This will cause the program to slow down much like `setVisible(false);`. – tckmn Jan 03 '13 at 12:04
  • @Doorknob According to `JFrame`'s doc( http://docs.oracle.com/javase/6/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation(int) ), `DISPOSE_ON_CLOSE`: Automatically hide and **dispose** the frame after invoking any registered WindowListener objects, which is different from `HIDE_ON_CLOSE` – Hui Zheng Jan 03 '13 at 12:13
  • Dispose does not mean "free up resources." Try making a program that makes tons of `JFrame`s and disposes them, it will slow down a LOT. – tckmn Jan 03 '13 at 12:16
  • 1
    @Doorknob *"Try making a program that makes tons of `JFrame`s"* 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!). – Andrew Thompson Jan 04 '13 at 00:14
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.

helvete
  • 2,455
  • 13
  • 33
  • 37
Fabik
  • 1
  • 1