I have a program with a JFrame that I use a WindowListener to close the program with. I use the following method to prompt a message about saving changes made in the program:
public void windowClosing(WindowEvent e) {
if (condition) {
System.exit(0);
}
However, when I press cmd + Q, my program will quit without me having the option of saving. Is there a smart way to make sure that I can have a condition before I close my program regardless if I close it through the window X or through my keyboard short commands? Or do I need to create a KeyEvent for this?
You need to change the DefaultCloseOperation on JFrame and then dispose() the frame on windowClosing event (or System.exit(0) like you show above). Here is a simple working example:
This way all closing operation will use your condition.