Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create custom operation for setDefaultCloseOperation?

I want to make my Java Application to call my own custom made function when the "close" cross button is pressed. as far as i see there may be no way since setDefaultCloseOperation has no overloads at all.

Any idea how this can be achieved?

like image 242
prometheuspk Avatar asked May 21 '11 19:05

prometheuspk


People also ask

How do you use setDefaultCloseOperation?

The default behavior is to simply hide the JFrame when the user closes the window. To change the default behavior, you invoke the method setDefaultCloseOperation(int) . To make the JFrame behave the same as a Frame instance, use setDefaultCloseOperation(WindowConstants. DO_NOTHING_ON_CLOSE) .

What is the use of setDefaultCloseOperation in Java?

Calling setDefaultCloseOperation(EXIT_ON_CLOSE) causes the application to exit when the application receives a close window event from the operating system. Pressing the close (X) button on your window causes the operating system to generate a close window event and send it to your Java application.

What is Setlayout null in Java?

null layout means absolute positioning - you have to do all the work in your code. No layout manager to help you out.

What is dispose on close?

DISPOSE_ON_CLOSE will call dispose() on the frame, which will make it disappear and remove the resources it is using. You cannot bring it back, unlike hiding it.


4 Answers

maybe this one, but before that read tutorial WindowListener posted by Howard, there are some/another options

WindowListener exitListener = new WindowAdapter() {

    @Override
    public void windowClosing(WindowEvent e) {
        int confirm = JOptionPane.showOptionDialog(
             null, "Are You Sure to Close Application?", 
             "Exit Confirmation", JOptionPane.YES_NO_OPTION, 
             JOptionPane.QUESTION_MESSAGE, null, null, null);
        if (confirm == 0) {
           System.exit(0);
        }
    }
};
frame.addWindowListener(exitListener);
like image 150
mKorbel Avatar answered Sep 30 '22 11:09

mKorbel


All the above suggestions are correct in that you need to use a WindowListener.

However all the answer are also incomplete in that they forget to mention that you may also want to add:

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(...);

This will allow your code to take full control of the window closing process as the window will not automatically close unless you tell it to (generally by using the dispose() method on the frame). This allow you to promt the user for a confirmation to close the window or not.

Closing an Application has a simple API that allows you to create a simple Action that is executed when the window is closed. It manages the close operation and the window listener code for you.

like image 32
camickr Avatar answered Sep 30 '22 12:09

camickr


You can add a window listener for the frame:

frame.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent evt) {
     onExit();
   }
  });

...
public void onExit() {
  System.err.println("Exit");
  System.exit(0);
}
like image 35
khachik Avatar answered Sep 30 '22 11:09

khachik


You can add a WindowListener to your frame (see e.g. here).

like image 28
Howard Avatar answered Sep 30 '22 11:09

Howard