I am working on swing based application after log-in i have a menu window containing different tabs which open different windows but when i close one of them close all windows automatically.Guide me how to solve it? thanks.
2 Answers
You are calling setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE)
so your whole application will close when this window is closed. This is because (as it says below) System.exit();
will be called.
You should pass one of the other values to setDefaultCloseOperation(int operation)
depending on the needs of your application.
In any case please read The Use of Multiple JFrames, Good/Bad Practice? if you are using multiple frames.
Source setDefaultCloseOperation
public void setDefaultCloseOperation(int operation)
Sets the operation that will happen by default when the user initiates a "close" on this frame. You must specify one of the following choices:
DO_NOTHING_ON_CLOSE
(defined in WindowConstants): Don't do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object.
HIDE_ON_CLOSE
(defined in WindowConstants): Automatically hide the frame after invoking any registered WindowListener objects.
DISPOSE_ON_CLOSE
(defined in WindowConstants): Automatically hide and dispose the frame after invoking any registered WindowListener
objects.
EXIT_ON_CLOSE
(defined in JFrame): Exit the application using the System exit method. Use this only in applications.The value is set to HIDE_ON_CLOSE by default. Changes to the value of this property cause the firing of a property change event, with property name "defaultCloseOperation".

- 1
- 1

- 7,734
- 9
- 41
- 60
Normally you should use HIDE_ON_CLOSE. If you would like to open the frame again you could do:
mainFrame mF = new mainFrame();
mF.setVisible(true);
mainFrame being the name of the GUI class.

- 689
- 5
- 22