JDialog from Icon close

E

emekadavid

I have a window, JFrame, if I turn off setDefaultCloseOperation(),
and register a listener on the window, I want to bring up a JDialog or
Optionpane that gives yes/cancel option if I attempt to close the
window by clicking on the icon at the top left or the X button at top
right.
 
R

RedGrittyBrick

emekadavid said:
I have a window, JFrame, if I turn off setDefaultCloseOperation(),
and register a listener on the window, I want to bring up a JDialog or
Optionpane that gives yes/cancel option if I attempt to close the
window by clicking on the icon at the top left or the X button at top
right.

WindowListener windowCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
JFrame frame = (JFrame) e.getWindow();
int choice = JOptionPane.showConfirmDialog(frame,
"Really Exit MyApp?",
"Exiting MyApp ... ",
JOptionPane.OK_CANCEL_OPTION);
if (choice == 0) {
// savePreferences();
System.exit(0);
}
}
};
addWindowListener(windowCloser);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top