66

I don't get how can I employ this code:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

to close the program with the x button.

4
  • 6
    Unfortunately the terrifying part about using JFrame.EXIT_ON_CLOSE is that once all the jframes are closed, it basically calls a System.exit(0) which means it kills all other outstanding threads! whoa!
    – rogerdpack
    May 17, 2012 at 19:43
  • 2
    If you're using a Frame (Class Extends Frame) this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Mar 8, 2016 at 10:07
  • @rogerdpack If you know you have outstanding threads, you can catch the "click on close" event and pop a confirmation message before allowing it to kill them right ? Jul 4, 2022 at 5:01
  • Probably. The click on close event from which window? How does it know it's the last, and the other threads are still running? Isn't trivial...
    – rogerdpack
    Jul 5, 2022 at 6:06

8 Answers 8

99

You need the line

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

Because the default behaviour for the JFrame when you press the X button is the equivalent to

frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);

So almost all the times you'll need to add that line manually when creating your JFrame

I am currently referring to constants in WindowConstants like WindowConstants.EXIT_ON_CLOSE instead of the same constants declared directly in JFrame as the prior reflect better the intent.

4
  • 2
    +1 for identifying the correct default action/behavior of a frame. OTOH it is generally better to ensure that no other non-daemon threads are running, and set the frame exit action as JFrame.DISPOSE_ON_CLOSE. Oct 17, 2011 at 21:58
  • My ide is telling me it should be WindowConstants.EXIT_ON_CLOSE Apr 26, 2013 at 16:59
  • 1
    That is OK, you can use both because they have the same integer values for each constants but, yes, WindowConstants interface have been introduced to replace constants declared directly in JFrame as it reflects the intent better May 3, 2013 at 19:49
  • 1
    If you're using a Frame (Class Extends Frame) than use this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Mar 8, 2016 at 10:07
7

If you don't have it, the JFrame will just be disposed. The frame will close, but the app will continue to run.

5
  • 1
    Perhaps the OP would like to experiment with this code which creates 3 frames that set JFrame.DISPOSE_ON_CLOSE. The behavior is different to when JFrame.EXIT_ON_CLOSE is set. Oct 17, 2011 at 21:51
  • 3
    Actually I think jaime is the only one that has got it entirely right so far. If a frame is disposed and there are no non-daemonn threads running, the JRE will exit. Oct 17, 2011 at 21:56
  • @Andrew Thompson please see my IDE_relevant post
    – mKorbel
    Oct 17, 2011 at 22:11
  • @Andrew Thompson ahhh you are right I lost Dispose, I deleted my pip - pip - pip
    – mKorbel
    Oct 17, 2011 at 22:24
  • According to the javadoc setDefaultCloseOperation "Note: When the last displayable window within the Java virtual machine (VM) is disposed of, the VM may terminate. See AWT Threading Issues for more information." So, it doesn't say there is a guarantee, but it may. Oct 18, 2011 at 12:22
5

Calling setDefaultCloseOperation(EXIT_ON_CLOSE) does exactly this. It 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. The close window event is processed by the AWT event loop in your Java application which will exit the application in response to the event.

If you do not call this method the AWT event loop may not exit the application in response to the close window event but leave it running in the background.  

2
  • 1
    It was. I just fixed it a minute ago Oct 17, 2011 at 22:02
  • Thanks for editing. Unfortunately I cannot seem to either reverse the down-vote or add an up-vote, since the site software is claiming it was not edited. :( Oct 17, 2011 at 22:06
3

I spent quite a bit of time spelunking through the internet for an elegant solution to this. As is usually the case, I found a lot of conflicting information.

I finally ended with:

  1. Do not use EXIT_ON_CLOSE as this can leave resources behind;
  2. Do use something like the following in the JFrame initialization:

    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    
  3. The real discovery was how to actually dispatch a window message to the JFrame. As an example, as part of your JMenuItem for exiting the application, use the following, where the function getFrame() returns a reference to the JFrame:

    public class AppMenuFileExit extends JMenuItem implements ActionListener
    {
        // do your normal menu item code here
    
          @Override
          public void actionPerformed(ActionEvent e)
          {
            WindowEvent we;
            we = new WindowEvent((Window) App.getFrame(), WindowEvent.WINDOW_CLOSING);
    
            App.getFrame().dispatchEvent(we);
          }
    }
    

    JFrame is a subclass of Window so may be cast to Window for this purpose.

  4. And, have the following in your JFrame class to handle Window messages:

    public class AppFrame extends JFrame implements WindowListener
    {
      // Do all the things you need to for the class
    
      @Override
      public void windowOpened(WindowEvent e)
      {}
      @Override
      public void windowClosing(WindowEvent e)
      {/* can do cleanup here if necessary */}
    
      @Override
      public void windowClosed(WindowEvent e)
      {
        dispose();
        System.exit(0);
      }
      @Override
      public void windowActivated(WindowEvent e)
      {}
      @Override
      public void windowDeactivated(WindowEvent e)
      {}    
      @Override
      public void windowDeiconified(WindowEvent e)
      {}
      @Override
      public void windowIconified(WindowEvent e)
      {}
    }
    
1

If you're using a Frame (Class Extends Frame) you'll not get the

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
1
  • 1
    If you're using a Frame (Class Extends Frame) this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Mar 8, 2016 at 10:10
1

If you don't extend JFrame and use JFrame itself in variable, you can use:

frame.dispose();
System.exit(0);
0

The following code works for me:

System.exit(home.EXIT_ON_CLOSE);
0

this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

this worked for me in case of Class Extends Frame

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.