JFrame Exit on close Java

JavaJframe

Java Problem Overview


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

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

to close the program with the x button.

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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

Solution 3 - Java

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.  

Solution 4 - Java

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)
      {}
    }
    

Solution 5 - Java

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

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)

Solution 6 - Java

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

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

Solution 7 - Java

The following code works for me:

System.exit(home.EXIT_ON_CLOSE);
    

Solution 8 - Java

this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

this worked for me in case of Class Extends Frame

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser983246View Question on Stackoverflow
Solution 1 - JavaJaime HablutzelView Answer on Stackoverflow
Solution 2 - JavaJeff StoreyView Answer on Stackoverflow
Solution 3 - JavaBrian ColemanView Answer on Stackoverflow
Solution 4 - JavaHiggsView Answer on Stackoverflow
Solution 5 - JavaYacineView Answer on Stackoverflow
Solution 6 - JavaDmitriy PotapovView Answer on Stackoverflow
Solution 7 - JavaEmilio Yanguas QuirogaView Answer on Stackoverflow
Solution 8 - JavaAditya JoshiView Answer on Stackoverflow