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...
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.
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.
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.
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().
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With