Solved

Close window confirmation dialog

Posted on 2003-10-26
2
359 Views
Last Modified: 2008-02-01
When the user presses the X button at the top-right corner of the main frame I want him to confirm that he intends to close that frame.
So using windowClosing(WindowEvent e) and windowClosed(WindowEvent e) I display a confirmation dialog (JOptionPane) - the problem is that even when he chooses "No" the frame is being closed.
Doing the same from File->Exit menu works just fine obviously.
0
Comment
Question by:s_lavie
2 Comments
 
LVL 3

Accepted Solution

by:
savalou earned 50 total points
ID: 9622394
Have you tried to consume() the event?  

Here's some info on an alternate approach:
http://java.sun.com/docs/books/tutorial/uiswing/components/frame.html#windowevents
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9622405
The following combo will do the trick:

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
            int returnValue = JOptionPane.showConfirmDialog(null, "Are you sure you want to close?");
            if (returnValue == JOptionPane.YES_OPTION) {
                  System.exit(0);
            }
      }
});
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question