tags:

views:

23

answers:

2

How to prevent to close Java swing Application, when user clicks on close button?

+2  A: 

frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); on your main frame should prevent that.

The setDefaultCloseOperation(int) method allows you to choose what to do when the user closes the JFrame:

  • 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.

Gnoupi
+1  A: 
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

frame.addWindowListener(new WindowAdapter() {
// handle window closing 
});
PeterMmm