CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 1999
    Posts
    492

    Rejecting the EXIT of a Java app

    I know this is simple, but I just can't find the solution (and Code Guru's search feature seems to be broken as clicking on it produces an endless wait)

    I want the ability to reject the exiting of an app. When the user exists, I want to display a message that allows the user to cancel the exit request.

    I have had no luck. First off, this has such vague, general words that search results produce little more than completely unrelated topics. Other results advise listening for the windowClosing() event provided by WindowAdapter / WindowListener. This fails for 2 reasons:

    1) Closing the app with "System.exit( X )" does not result in this event being fired.

    2) windowClosing() does not allow you to reject the closing. All it does is tell you that it happened (when it even bothers to tell you that), but there is no way to reject it.


    What do I need to do?
    Why are the "tolerant" so easy to offend?

  2. #2
    Join Date
    Aug 1999
    Posts
    492

    Re: Rejecting the EXIT of a Java app

    Finally found the solution:

    http://tips4java.wordpress.com/2009/...n-application/

    Basically in the windowClosing() event you don't reject the closing you just change how the app handles a closing request. This ends up allowing you to either accept or reject. So you'd have something similar to this:

    Code:
        public void windowClosing() {
           
            final int ret = JOptionPane.showConfirmDialog(this, "Are you sure you want to quit?",
                                                                "Exit",
                                                                JOptionPane.YES_NO_OPTION,
                                                                JOptionPane.INFORMATION_MESSAGE);
            
            if (ret == JOptionPane.YES_OPTION) {
                
                setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            }
            else {
    
                setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
            }
        }
    Why are the "tolerant" so easy to offend?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured