Solved

JFrame, closing window button.

Posted on 2004-04-03
4
446 Views
Last Modified: 2011-09-20
Hi.

I want to handle the event for when the user clicks the close button (top right hand corner on windows machine) on a JFrame. I am doing this:

        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                int returnVal = JOptionPane.showConfirmDialog(f,
                                                              "Are you sure you want to exit?",
                                                              "Notepad",
                                                              JOptionPane.YES_NO_OPTION,
                                                              JOptionPane.QUESTION_MESSAGE);
               
                if (returnVal == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
            }
        });

But the JFrame still closes even when i press the "NO" button from the dialog which pops up. Any ideas on how i can NOT make it close the program if the user presses on the "NO" button in the dialog?

Cheers, Zephyr__.
0
Comment
Question by:Ravi Singh
[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
  • 2
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 30 total points
ID: 10749862
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
0
 
LVL 15

Assisted Solution

by:Javatm
Javatm earned 20 total points
ID: 10749909
Because you dont do anything after your if statement it should be :

f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                int returnVal = JOptionPane.showConfirmDialog(f,
                                                              "Are you sure you want to exit?",
                                                              "Notepad",
                                                              JOptionPane.YES_NO_OPTION,
                                                              JOptionPane.QUESTION_MESSAGE);
               
                if (returnVal == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
                else if returnVal == JOptionPane.NO_OPTION) {
                    Do something here . . .
            }
        });
0
 
LVL 92

Expert Comment

by:objects
ID: 10750219
0
 
LVL 15

Expert Comment

by:Javatm
ID: 10750353
http://www.freewebs.com/cube-j

Always glad to help . . .
Friend : Javatm
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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 …
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 …
Suggested Courses

623 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