Solved

JFrame, closing window button.

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
null output 3 59
ejb stateless example 2 68
Print Rhino Java Array in Javascript 1 58
restrict decimal places for double datatype 10 49
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

751 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