?
Solved

Close window confirmation dialog

Posted on 2003-10-26
2
Medium Priority
?
362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

771 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