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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
user983246
  • 1,429
  • 6
  • 19
  • 20
  • 7
    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 '12 at 19:43
  • 2
    If you're using a Frame (Class Extends Frame) this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); – Jaimin Patel Mar 08 '16 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 ? – Mauricio Gracia Gutierrez Jul 04 '22 at 05: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 05 '22 at 06:06

8 Answers8

100

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.

Jaime Hablutzel
  • 6,117
  • 5
  • 40
  • 57
  • 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`. – Andrew Thompson Oct 17 '11 at 21:58
  • My ide is telling me it should be **WindowConstants**.EXIT_ON_CLOSE – Daniel Kaplan Apr 26 '13 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 – Jaime Hablutzel May 03 '13 at 19:49
  • 1
    If you're using a Frame (Class Extends Frame) than use this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); – Jaimin Patel Mar 08 '16 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.

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
  • 1
    Perhaps the OP would like to experiment with [this code](http://stackoverflow.com/questions/7143287/how-to-best-position-swing-guis/7143398#7143398) which creates 3 frames that set `JFrame.DISPOSE_ON_CLOSE`. The behavior is different to when `JFrame.EXIT_ON_CLOSE` is set. – Andrew Thompson Oct 17 '11 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. – Andrew Thompson Oct 17 '11 at 21:56
  • @Andrew Thompson please see my IDE_relevant post – mKorbel Oct 17 '11 at 22:11
  • @Andrew Thompson ahhh you are right I lost Dispose, I deleted my pip - pip - pip – mKorbel Oct 17 '11 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. – Jeff Storey Oct 18 '11 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.  

Alex Weitz
  • 3,199
  • 4
  • 34
  • 57
Brian Coleman
  • 1,070
  • 5
  • 4
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)
      {}
    }
    
TT.
  • 15,774
  • 6
  • 47
  • 88
Higgs
  • 132
  • 1
  • 2
1

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

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
Emil
  • 7,220
  • 17
  • 76
  • 135
Yacine
  • 11
  • 1
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);
simonmorley
  • 2,810
  • 4
  • 30
  • 61
0

this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

this worked for me in case of Class Extends Frame