Java – how to hide JFrame correctly
I have a very simple JFrame window with a button: No
In the main function, I set setvisible (true); For my JFrame and no button listeners, I want to close the window, so I set the visibility to false: setvisible (false); Then I do system exit(0); To prevent possible memory leaks when running the program multiple times
I have two questions:
>I really need system exit(0); In the above case? > If I use this JFrame as a pop-up window, I can't actually use system exit(0); Because this will terminate the whole program So how to properly close the pop-up window and keep it in the main JFrame window? (now I just use setvisible (false) to close it; When I do this many times through the program, the program becomes very slow
Solution
>Using CardLayout
>Use jdialog with parent to JFrame, setmodal / modalitytypes > create only one jdialog and to reuse this one jdialog by getcontentpane #removeall() > use joptionpane for simple user interaction
>Put the two together, higher than two points, and use CardLayout to pop up jdialog and parent to JFrame. Note that jdialog may need to be called after switching from one card to another pack()