Basic Java Swing, how to exit and dispose of your application/JFrame
Asked Answered
I

4

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

}
Ionian answered 16/8, 2011 at 15:18 Comment(2)
Please check out Andrew's answer here: awt-window-close-listener-eventSchutzstaffel
Possible duplicate of How to quit a java app from within the programNull
A
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();
            }
        });
    }
}
Abbieabbot answered 16/8, 2011 at 17:5 Comment(3)
Is this the standard (good way) to handle on close events. Looks good.Ionian
@Berlin Brown I hope that yes, works for me as I expected, but in Swing not possible declare two or more download.oracle.com/javase/tutorial/uiswing/components/… 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 :-)Abbieabbot
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.Ionian
B
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.

Bessiebessy answered 16/8, 2011 at 15:21 Comment(4)
How is that different than EXIT_ON_CLOSEIonian
@Berlin, EXIT_ON_CLOSE terminates the application, whereas DISPOSE_ON_CLOSE only disposes of the JFrame instance.Segregate
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.Beamends
Exactly. @Berlin, Take a look at that link to the Java tutorials; it enumerates the differences.Bessiebessy
P
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.

Periodic answered 16/8, 2011 at 15:23 Comment(0)
D
1
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JFrame.setDefaultCloseOperation(int operation)

JFrame.EXIT_ON_CLOSE

Decennary answered 16/8, 2011 at 15:21 Comment(5)
This actually uses System.exit(0).Bessiebessy
Is there an event handler for frame.setDefaultCloseOperation? E.g. is there a method I can override? onExit() { } or something similar.Ionian
@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.Bessiebessy
But if I wanted to, I can use window listener.Ionian
@Berlin: Yup, you can do something else with the window listener if you want to.Bessiebessy

© 2022 - 2024 — McMap. All rights reserved.