I have following code snippet:
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int result = JOptionPane.showConfirmDialog(Bookstore.this, "Are you sure to quit?", "Confirm", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
// release connection resource
if (bookstoreConnection != null) {
bookstoreConnection.closeConnection();
}
// JFrame handles close request based on the property
// set by invoking the setDefaultCloseOperation(...)
Bookstore.this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
});
and whenever i press [x] button at upper right corner the frame disappears regardless of whatever option i chose. In this case I want to know how to retain frame window if i choose NO option. Thank you!
Use this
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)as the default close operation on your JFrame subclass (I don’t remember if this is the default if you don’t explicitly add thesetDefaultCloseOperation(...)with another value)http://download.oracle.com/javase/1.3/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation(int)