I have a simple java graphical application that tells the user a message like "Are you sure you want to exit?" Before he leaves the program. Although this only works when I use my JButton exit program, but when I use the red cross in the JFrame title bar, it doesnβt matter, I click on the βYesβ or βNoβ button in the message dialog box.
For this task, I added a new WindowListener to my JFrame, with this code
frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { int Answer = JOptionPane.showConfirmDialog(frame, "You want to quit?", "Quit", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (answer == JOptionPane.YES_OPTION) { System.exit(0); } } });
If I click no, the program will still exit, how can I stop this action?
source share