Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: closing a program

  1. #1
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default closing a program

    I hope I have posted this question in the right place ...

    I have a swing-based program where I have set its behavior on closing to do nothing:

    frame.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );

    Instead, I have add a window listener to the JFrame to handle closing the program:

    frame.addWindowListener( new WindowAdapter()
    {
    public void windowClosing( WindowEvent we )
    {
    if ( loggedIn )
    {
    logOutDlg.setLocationRelativeTo( frame );
    logOutDlg.setVisible( true );
    }
    else System.exit( 0 );
    }
    } );

    where the actual log-out behavior is controlled in a log-out dialog (logOutDlg).

    This all works great.

    Here is the issue.

    If the user presses <control>q, the program closes, but the log-out dialog is ignored. This leads to some undesirable behavior regarding databases accessed by the program.

    How do I control this meta-behavior? That is to say, how do I cause the log-out dialog to be displayed when the users presses <control>q?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: closing a program

    Does <CTL>Q cause a WindowEvent that your listener sees?

  3. #3
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default Re: closing a program

    No. It doesn't appear so.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: closing a program

    I suggest that you create an SSCCE that demonstrates your problem, something along the lines of this code below. Then we can test your program and try to help you fix it.

    Java Code:
    import java.awt.Dimension;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class WindowClosing {
    
       public static void main(String[] args) {
          final JFrame frame = new JFrame("WindowClosing");
          frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
          frame.setPreferredSize(new Dimension(400, 300));
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
    
          frame.addWindowListener(new WindowAdapter() {
             @Override
             public void windowClosing(WindowEvent arg0) {
                reallyClose();
             }
    
             @Override
             public void windowClosed(WindowEvent arg0) {
                reallyClose();
             }
    
             private void reallyClose() {
                int result = JOptionPane.showConfirmDialog(frame,
                      "Do you want to close this application?", "Are You Sure?",
                      JOptionPane.OK_CANCEL_OPTION);
                if (result == JOptionPane.OK_OPTION) {
                   System.exit(0);
                }
             }
    
          });
       }
    }

  5. #5
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default Re: closing a program

    Will do.

  6. #6
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default Re: closing a program

    Fubarable,

    Your code demonstrates the problem perfectly. If you run it and press control-Q, the program exits without accessing the methods in the WindowListener.

    By the way, can you explain why there are two windowClosing() methods?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: closing a program

    What OS are you on? <CTRL>+Q does nothing on my Windows XP system.
    Is there anything in your code to handle <CTRL+Q?

  8. #8
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default Re: closing a program

    Mac OS X. Pressing control-Q closes the program. Normally, I wouldn't care, but this program basically accesses remote databases. If my little log-out routine is not run, the database tables are not cleaned up.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: closing a program

    Is Ctrl+Q the same as Ctrl+Alt+Del on a PC? That kills the JVM, not just close the window.
    I think there are methods that are called when the JVM is being cancelled.

  10. #10
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default Re: closing a program

    Boy, I don't know about your PC question. I don't have a Windows box to try it out.

    When I start my code (or Fubarable's - great username!), the items of the menu bar (along the top of the desktop) are replaced with a drop down menu with the program's name. One of the items in this drop down menu is a Quit Program option, which has the key combination ... nuts ... it's command-Q, not control-Q. (It's the key to the left of the space bar.) This ket sequence, command-Q, it the traditional way of closing programs in Apple Land.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: closing a program

    I'm not an Applephile so I can't help you here, sorry. :(

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: closing a program

    There's a suggestion here that might help.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default Re: closing a program

    I'll look into it.

    Thanks.

  14. #14
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default Re: closing a program

    Thanks a lot, Darryl. That did the trick.

Similar Threads

  1. frame closing
    By manish007g in forum AWT / Swing
    Replies: 4
    Last Post: 09-24-2011, 11:15 PM
  2. closing JFrame without terminating program
    By chirag123 in forum AWT / Swing
    Replies: 5
    Last Post: 04-25-2011, 02:10 PM
  3. BufferedReader closing by itself (not what i want)
    By Sneaky Fox in forum Threads and Synchronization
    Replies: 2
    Last Post: 02-27-2011, 06:43 AM
  4. closing FileOutputStream?
    By mgrootsch in forum New To Java
    Replies: 1
    Last Post: 05-17-2010, 06:36 PM
  5. Replies: 5
    Last Post: 06-26-2008, 04:16 AM

Posting Permissions

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