Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing on a single window closes all the frames in java

Tags:

java

swing

frame

I would like to make possible the navigation of frames in java.Whenever i close a frame the remaining frames which are also opened get closed;and the entire program stops.

Please help...

like image 588
rak Avatar asked Mar 28 '12 11:03

rak


People also ask

How do I close a single window in a JFrame?

You can use setVisible(false) on your JFrame if you want to display the same frame again. Otherwise call dispose() to remove all of the native screen resources.

Can we close frame window?

We can close the AWT Window or Frame by calling dispose() or System. exit() inside windowClosing() method. The windowClosing() method is found in WindowListener interface and WindowAdapter class.

How do I close a JFrame without exiting?

DISPOSE_ON_CLOSE -- The frame will be closed and disposed but the application will not exit. JFrame. DO_NOTHING_ON_CLOSE -- The frame will be closed but not disposed and the application will not exit.

Which method is used to close the JFrame?

You can close the frame programmatically by sending it the WINDOW_CLOSING event, like this: WindowEvent closingEvent = new WindowEvent(targetFrame, WindowEvent. WINDOW_CLOSING); Toolkit. getDefaultToolkit().


3 Answers

You probably used

   //this will terminate or exit your application    
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Maybe you want to use this instead,

   setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

for your reference go to this link

like image 134
Mr. Xymon Avatar answered Sep 28 '22 03:09

Mr. Xymon


If you want to close only that one frame, you should do something like this: setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)

If you want to close all frames whenever a single frame closes you can do the following:

You could use a window listener and call System.exit(0); when the JFrame closes, or try setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); on each JFrame. That way your program would close all frames and end.

If you need to perform some tasks before application quits, you should probably use the window listener.

like image 38
Paaske Avatar answered Sep 28 '22 03:09

Paaske


You can also do that graphicaly. right click on your frame and select properties and from there you can change that like the below picture.

enter image description here

like image 30
Ashraf Gardizy Avatar answered Sep 28 '22 02:09

Ashraf Gardizy