?
Solved

Close window confirmation dialog

Posted on 2003-10-26
2
Medium Priority
?
364 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 200 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

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