Closing the jframe without using the close(X) button and terminating the JVM

jframe

i have a frame that instantiates another frame but i don't want to use the close(x) button on the instantiated frame., so i created a button. how do i code that this button can be used to close the instantiated frame without quitting the JVM.

Best Solution

Having your own close button is weird UI and should be avoided.

To get rid of a frame when your own button is clicked you can just do:

jFrame.setVisible(false);

or

jFrame.dispose();

if you want to get rid of it completely.

On frames you don't want to exit the JVM on the close button being clicked specify:

jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

or

jFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

Depending on which of the behaviour you want.

Related Question