简体   繁体   中英

On button click, close the application Java GUI

I have a java application, want to close the GUI with confirmation window to close the application

for example:-

        frmViperManufacturingRecord.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frmViperManufacturingRecord.addWindowListener( new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                JFrame frame = (JFrame)e.getSource();

                int result = JOptionPane.showConfirmDialog(frame, "Are you sure you want to close the application?", "Please Confirm",JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION)
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
            });

This is working fine , when I press the window close (x) button, but I want to bring this event to a button to perform the action 'On Click', since I am newbie finding difficulties to bring it inside the 'actionPerformed'

So far I have tried the code below and it didn't work...

        //close window button
        JButton btnCloseWindow = new JButton("Close Window");
        btnCloseWindow.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //frmViperManufacturingRecord.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                //frmViperManufacturingRecord.dispose();
                //System.exit(0);
                JFrame frame = (JFrame)e.getSource();

                int result = JOptionPane.showConfirmDialog(frame, "Are you sure you want to close the application?", "Please Confirm",JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION)
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
        });

Please give me some directions on this, thanks

Change this:

if (result == JOptionPane.YES_OPTION)
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

to

if (result == JOptionPane.YES_OPTION)
  System.exit(0);
}

frame.setDefaultCloseOperation is what happens when someone clicks the 'x' to close the window. Every other way to exit is controlled by you. The best way is to have your window closing listener and action listener call the same method that will pop up the dialog and call System.exit(0) if the user wants to exit. This will also help you with cleanup operations.

Sample code:

public class Test extends JPanel implements WindowListener {
    public Test() {
        setLayout(new BorderLayout());
        add(new JLabel("This is a test."), BorderLayout.CENTER);
        JButton b = new JButton("Exit");
        b.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                exit();

            }
        });
        add(b, BorderLayout.SOUTH);
    }
    public static void main(String[] args) throws Exception {
        JFrame f = new JFrame();
        Test t = new Test();
        f.add(t, BorderLayout.CENTER);
        f.addWindowListener(t);
        f.pack();
        f.setVisible(true);
    }
    public void windowClosing(WindowEvent e) {
        exit();

    }

    private void exit() {
        System.exit(0);
    }
}

You can try:

if (result == JOptionPane.YES_OPTION){
                    frame.dispose();
            }

Also note CastException on the line 122.

Instead of

JFrame frame = (JFrame)e.getSource();

change to:

JFrame frame = new JFrame();

if still not working then try

frame.setVisible(false);
frame.dispose();

in if(result == JOptionPane.YES_OPTION){} block

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM