Solved

Close window confirmation dialog

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

746 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

11 Experts available now in Live!

Get 1:1 Help Now