setDefaultCloseOperation to show a JFrame instead
Asked Answered
G

3

7

I am making a word processor application in order to practise Java and I would like it so that when the user attempts to close the appliction, a JFrame will come up asking to save changes.

I was thinking about setDefaultCloseOperation() but I have had little luck so far. I would also like it to appear whent he user clicks the "X" on the top right of the window aswell if possible.

Goldeye answered 31/8, 2012 at 7:46 Comment(0)
V
15

You can set the JFrame DefaultCloseOperation to something like DO_NOTHING, and then, set a WindowsListener to grab the close event and do what you want. I'll post an exemple in a few minutes .

EDIT: Here's the example :

public static void main(String[] args) {
        final JFrame frame = new JFrame("Test Frame");

        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        frame.setSize(800, 600);

        frame.addWindowListener(new WindowAdapter() {
            //I skipped unused callbacks for readability

            @Override
            public void windowClosing(WindowEvent e) {
                if(JOptionPane.showConfirmDialog(frame, "Are you sure ?") == JOptionPane.OK_OPTION){
                    frame.setVisible(false);
                    frame.dispose();
                }
            }
        });

        frame.setVisible(true);
    }
Vladimar answered 31/8, 2012 at 7:49 Comment(5)
1) ITYM frame.addWindowListener(new WindowAdapter() { (otherwise it is a compilation error). 2) The JRE will not end since the EDT is still running. Here is one of the rare occasions where calling System.exit(0) might be necessary.Curio
@AndrewThompson what would you suggest being the safest way to close an application instead of System.exit(0); ?Goldeye
Well, you might 1st look at the running threads and check that it is only the EDT still running. If so, it would be quite safe to end the JVM using System.exit(0). But there is a better solution here, see my answer..Curio
@AndrewThompson 1) WindowListener is working fine and is the argument type defined in addWindowListener. 2) EDT stops after the frame has been disposed (and the JRE properly ends), but perhaps there are some case for which it won't (never meet one).Vladimar
"there are some case for which it won't" E.G. my Oracle supplied JRE. "(never meet one)." Well come on over to my place! Bring beer..Curio
C
4
import java.awt.event.*;
import javax.swing.*;

public class QuickGuiTest {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                final JFrame frame = new JFrame("Test Frame");

                frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                frame.setSize(600, 400);
                frame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        int result = JOptionPane.showConfirmDialog(
                                frame, "Are you sure?");
                        if( result==JOptionPane.OK_OPTION){
                            // NOW we change it to dispose on close..
                            frame.setDefaultCloseOperation(
                                    JFrame.DISPOSE_ON_CLOSE);
                            frame.setVisible(false);
                            frame.dispose();
                        }
                    }
                });
                frame.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Curio answered 31/8, 2012 at 8:38 Comment(0)
H
3
  • You have to add a WindowListener to the JFrame.

  • Inside the windowClosing method, you can provide required code.

For example:

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.OK_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.OK_OPTION) {
                System.exit(0);
            }
        }
    }

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

            @Override
            public void run() {
                ClosingFrame cf = new ClosingFrame();
            }
        });
    }
}
Hamblin answered 31/8, 2012 at 7:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.