Solved

Close window confirmation dialog

Posted on 2003-10-26
2
357 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split string containing \r\n in Java 46 38
oracle 11g 23 50
How to convert String matching to regex in java 4 21
expectj telnet failing 5 26
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now