JFrame.setDefaultCloseOperation(int option) Example
JFrame.setDefaultCloseOperation() method is used to specify several options that may be taken when the user clicks the close button.
Think of a scenario where you have several JFrame windows that opens ass the user interacts with the applcation.
The user many want to close one JFrame window without exiting the whole application and thats where JFrame.setDefaultCloseOperation comes in.
Think of a scenario where you have several JFrame windows that opens ass the user interacts with the applcation.
The user many want to close one JFrame window without exiting the whole application and thats where JFrame.setDefaultCloseOperation comes in.
JFrame.setDefaultCloseOperation methods options
- JFrame.EXIT_ON_CLOSE-exits the application.
- JFrame.HIDE_ON_CLOSE-the frame is hidden and application stills keeps on running.
- JFrame.DISPOSE_ON_CLOSE-the object is disposed but application does not exit
- JFrame.DO_NOTHING_ON_CLOSE-the close click is ignored.
JFrame.setDefaultCloseOperation Example
[
import javax.swing.JFrame; public class JFrameExample extends JFrame{ JFrameExample() { super("JFrame Example"); setSize(400,400); setLayout(null); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String args[]) { new JFrameExample(); } }
]
Note:
Incase you may forget to call this method,by default JFrame.HIDE_ON_CLOSE is used.