Results 1 to 5 of 5

Thread: Prevent Application Quit

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    49

    Prevent Application Quit

    Hi, this is my application closing code. I have a confirmation dialog box to ask if the user wishes to close the application, what should I put if user decides not to quit the application?

    Code:
    public void windowClosing(WindowEvent we) {
    int answer ;
    answer = JOptionPane.showConfirmDialog(null,"Are you sure you want to exit?","Exit System",JOptionPane.YES_NO_OPTION) ;
    if (answer == 0) {
    System.out.println("You have successfully quit Restaurant System ver. 1.0 Built By: Lex Seetoh") ;
    System.exit(0) ;
    }
    else if (answer == 1) {
    System.exit(1) ;
    }
    }
    Please enlighten me.
    ______________________________________

    Warmest Regards,

    Lex
    ______________________________________



    :|: Don't Mess With The Predator :|:

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Prevent Application Quit

    You probably want to code it a little differently. In the windowClosing method, I would simply have System.exit(0);

    Somewhere in the program is where you need to prompt the user with the joptionpane, such as the click on a JMenuItem named exit. Then you simply test the values, and if yes is clicked then System.exit(0), if no, then there's no worry, program will continue.


    However, if you want to provide some message after,before, during closing then you can take a look at overriding some of these methods:
    public void windowClosing(WindowEvent e) { }
    public void windowOpened(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}

    You could possibly put a JOptionPane message dialog in the bold method, but not quit sure about that.

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Prevent Application Quit

    If you choose 1 then you don't want to do anything, right? Why add the exit code there? You probably don't need the elseif at all if I understand correctly :

    Code:
    public void windowClosing(WindowEvent we) {
    int answer ;
    answer = JOptionPane.showConfirmDialog(null,"Are you sure you want to exit?","Exit System",JOptionPane.YES_NO_OPTION) ;
    if (answer == 0) {
    System.out.println("You have successfully quit Restaurant System ver. 1.0 Built By: Lex Seetoh") ;
    System.exit(0) ;
    }
    }


    Has someone helped you? Then you can Rate their helpful post.

  4. #4

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    49

    Arrow Re: Prevent Application Quit

    Hi,

    But if I click "NO", the application still exits. I put the System.exit(1) ; just to exit it properly. But what should i put if i wish to let the application continue to run if the user selects "NO" ?
    ______________________________________

    Warmest Regards,

    Lex
    ______________________________________



    :|: Don't Mess With The Predator :|:

  5. #5
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Prevent Application Quit

    You're not understanding the windowClosing method correctly. When that method is reached, then something has ALREADY invoked the frame to close. You can put whatever joptionpane you want there, but the program is already processing a close. You need the code somewhere else. The only thing that would really make sense in those methods are a message dialog, not some prompt.

    Let's say if the user clicks an exit button you want to prompt the user if they really want to quit:
    Code:
    public void actionPerformed(lasjdlfksjaf)
    {
         if (event.getSource() == exitButton)
         {
              prompt user
                if (yes)  System.exit(0);  //makes a call to windowclosing
                if (no) do nothing
         }
    }

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