Solved

JFrame, closing window button.

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fibonacci ten numbers 4 45
Java: How do I open the default windows program for a given file type 3 41
pagenation logic how it is working in my code 1 58
Setup GlassFish 4 22
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

820 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