Solved

JFrame, closing window button.

Posted on 2004-04-03
4
425 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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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 …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

815 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

15 Experts available now in Live!

Get 1:1 Help Now