10

What is a good way to dispose of a JFrame with code like this? I want to handle the Window exit and window close.

I know we shouldn't use System.exit();

public class JavaCellularAutomataSquare {

  public static final String TITLE = "Cellular Automata - Squaring Example";

  private int maxWidth = 600;
  private int maxHeight = 600;

  public void launch() {    
    final JFrame frame = new JFrame(TITLE);   
    frame.setLocation(20, 20);    
    frame.setPreferredSize(new Dimension(maxWidth, maxHeight));
    frame.setResizable(false);
    frame.setFocusable(true);

    final JPanel panel = new JPanel();
    panel.setLocation(20, 20);
    panel.setVisible(true);
    panel.setPreferredSize(new Dimension(maxWidth, maxHeight));    
    panel.setFocusable(true);
    panel.setBackground(Color.white);

    // Panel setup, toggle visibility on frame
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);    
  }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Berlin Brown
  • 11,504
  • 37
  • 135
  • 203
  • 2
    Please check out Andrew's answer here: [awt-window-close-listener-event](http://stackoverflow.com/questions/7073412/awt-window-close-listener-event/7073630#7073630) – Hovercraft Full Of Eels Aug 16 '11 at 15:30
  • Possible duplicate of [How to quit a java app from within the program](https://stackoverflow.com/questions/2670956/how-to-quit-a-java-app-from-within-the-program) – james.garriss Dec 04 '17 at 17:35

4 Answers4

10
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ClosingFrame extends JFrame {

    private JMenuBar MenuBar = new JMenuBar();
    private JFrame frame = new JFrame();
    private static final long serialVersionUID = 1L;
    private JMenu File = new JMenu("File");
    private JMenuItem Exit = new JMenuItem("Exit");

    public ClosingFrame() {
        File.add(Exit);
        MenuBar.add(File);
        Exit.addActionListener(new ExitListener());
        WindowListener exitListener = new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                int confirm = JOptionPane.showOptionDialog(frame,
                        "Are You Sure to Close this Application?",
                        "Exit Confirmation", JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE, null, null, null);
                if (confirm == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
            }
        };
        frame.addWindowListener(exitListener);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setJMenuBar(MenuBar);
        frame.setPreferredSize(new Dimension(400, 300));
        frame.setLocation(100, 100);
        frame.pack();
        frame.setVisible(true);
    }

    private class ExitListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            int confirm = JOptionPane.showOptionDialog(frame,
                    "Are You Sure to Close this Application?",
                    "Exit Confirmation", JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null, null, null);
            if (confirm == JOptionPane.YES_OPTION) {
                System.exit(0);
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ClosingFrame cf = new ClosingFrame();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Is this the standard (good way) to handle on close events. Looks good. – Berlin Brown Aug 16 '11 at 17:21
  • @Berlin Brown I hope that yes, works for me as I expected, but in Swing not possible declare two or more http://download.oracle.com/javase/tutorial/uiswing/components/toplevel.html with setDefaultCloseOperation(EXIT_ON_CLOSE);, because each of them kill JVM instance, you have to accepted that or to set another with HIDE or DISPOSE :-) – mKorbel Aug 16 '11 at 17:32
  • I like this. I asked a kind of generic question but this was the response I was looking for. Also, the others were good but this had the code. – Berlin Brown Aug 16 '11 at 18:39
8

JFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE) frees up resources when the window is closed. You can see some of the other operations in the Java tutorials here.

The possible arguments that you can use in the method are defined in the WindowConstants interface, if you are curious about your options.

Feanor
  • 2,715
  • 4
  • 29
  • 43
  • How is that different than EXIT_ON_CLOSE – Berlin Brown Aug 16 '11 at 15:30
  • @Berlin, `EXIT_ON_CLOSE` terminates the application, whereas `DISPOSE_ON_CLOSE` only disposes of the `JFrame` instance. – mre Aug 16 '11 at 15:31
  • Exactly. @Berlin, Take a look at that link to the Java tutorials; it enumerates the differences. – Feanor Aug 16 '11 at 15:33
  • DISPOSE_ON_CLOSE will let the AWT thread exit when there are no non-disposed AWT components. That will be your last thread so the main thread will exit. – Clint Aug 16 '11 at 15:33
3

If you need to perform some operations while closing your application, propably you need a shut down hook. Have a look at this post.

Community
  • 1
  • 1
Heisenbug
  • 38,762
  • 28
  • 132
  • 190
1
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JFrame.setDefaultCloseOperation(int operation)

JFrame.EXIT_ON_CLOSE

Serabe
  • 3,834
  • 19
  • 24
  • This actually uses `System.exit(0)`. – Feanor Aug 16 '11 at 15:23
  • Is there an event handler for frame.setDefaultCloseOperation? E.g. is there a method I can override? onExit() { } or something similar. – Berlin Brown Aug 16 '11 at 15:27
  • @Berlin: Once you set the default close operation, there is no need to override a method. Unless you implement a window listener to do something different, the default close operation is performed when the user closes the window. – Feanor Aug 16 '11 at 15:28
  • But if I wanted to, I can use window listener. – Berlin Brown Aug 16 '11 at 15:29
  • 1
    @Berlin: Yup, you can do something else with the window listener if you want to. – Feanor Aug 16 '11 at 15:31