2
private void windowClosing(java.awt.event.WindowEvent evt)                               
    {                                   
        int confirmed = JOptionPane.showConfirmDialog(null, "Exit Program?","EXIT",JOptionPane.YES_NO_OPTION);
        if(confirmed == JOptionPane.YES_OPTION)
        {
            dispose();
        }
    }

I want to close program by pressing Close Window Button with confirmation...But when I choose "No" to back to my Jframe, it still helps me to exit the program???

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
jefferyleo
  • 630
  • 3
  • 17
  • 34
  • 3
    I'm sorry it's not clear what you're asking – Typo Jan 24 '14 at 10:51
  • 2
    For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve). – Andrew Thompson Jan 24 '14 at 10:52
  • Window there got 3 options right? 1 is minimize, 1 is expand, and 1 is close program...i want to make that close button when i press it, it come out a confirmation...and i cant do it... – jefferyleo Jan 24 '14 at 10:55
  • Check this SO answer : http://stackoverflow.com/a/19675197/1686291 – Not a bug Jan 24 '14 at 10:56
  • Possible duplicate of [Java - Message when closing JFrame Window](http://stackoverflow.com/questions/13419947/java-message-when-closing-jframe-window) – Autar Jan 07 '16 at 10:52

3 Answers3

6

From what i understand you want something like this

addWindowListener(new WindowAdapter() {
  public void windowClosing(WindowEvent e) {
    int confirmed = JOptionPane.showConfirmDialog(null, 
        "Are you sure you want to exit the program?", "Exit Program Message Box",
        JOptionPane.YES_NO_OPTION);

    if (confirmed == JOptionPane.YES_OPTION) {
      dispose();
    }
  }
});

If you want to use it on some button do similiar function to button. Put listener on it and do same. But I'm not sure if I get your question right. But If you want to use button use ActionListener and action performed method.

check question - Java - Message when closing JFrame Window

Remco32
  • 3
  • 1
Tomas Bisciak
  • 2,801
  • 5
  • 33
  • 57
  • But i cant add that "addWindowsListener" for myself..since the code is auto generated with my program – jefferyleo Jan 24 '14 at 11:00
  • What do you mean by that? You use Window builder? if yes then you still can modify and add anything you like to any of your gui parts – Tomas Bisciak Jan 24 '14 at 11:02
  • Should adding else under the if condition there if(confirmed == JOptionPane.YES_OPTION) { this.dispose(); } else { setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); } working well now – jefferyleo Jan 24 '14 at 11:10
5
JFrame frame = new JFrame();

// ...

frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent evt) {
        int resp = JOptionPane.showConfirmDialog(frame, "Are you sure you want to exit?",
            "Exit?", JOptionPane.YES_NO_OPTION);

        if (resp == JOptionPane.YES_OPTION) {
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        } else {
            frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        }
    }
});
John Doe
  • 111
  • 2
  • 14
Udeesha Induwara
  • 605
  • 1
  • 10
  • 20
1

Try this, it's working for me.

private void windowClosing(java.awt.event.WindowEvent evt) {                                   
    int confirmed = JOptionPane.showConfirmDialog(null, "Exit Program?","EXIT",JOptionPane.YES_NO_OPTION);
    if(confirmed == JOptionPane.YES_OPTION)
    {
        dispose();
    }
} else {
     setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Alper Akture
  • 2,445
  • 1
  • 30
  • 44
homesh
  • 11
  • 1