Solved

JFrame, closing window button.

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

707 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