Results 1 to 14 of 14
Thread: closing a program
- 01-10-2012, 10:58 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
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?
- 01-10-2012, 11:31 PM #2
Re: closing a program
Does <CTL>Q cause a WindowEvent that your listener sees?
- 01-11-2012, 12:14 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
Re: closing a program
No. It doesn't appear so.
-
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); } } }); } }
- 01-11-2012, 12:25 AM #5
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
Re: closing a program
Will do.
- 01-12-2012, 12:48 AM #6
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
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?
- 01-12-2012, 12:50 AM #7
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?
- 01-12-2012, 12:55 AM #8
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
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.
- 01-12-2012, 01:00 AM #9
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.
- 01-12-2012, 01:08 AM #10
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
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.
-
Re: closing a program
I'm not an Applephile so I can't help you here, sorry. :(
- 01-12-2012, 07:24 AM #12
Re: closing a program
There's a suggestion here that might help.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-12-2012, 03:28 PM #13
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
Re: closing a program
I'll look into it.
Thanks.
- 01-12-2012, 10:20 PM #14
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
Similar Threads
-
frame closing
By manish007g in forum AWT / SwingReplies: 4Last Post: 09-24-2011, 10:15 PM -
closing JFrame without terminating program
By chirag123 in forum AWT / SwingReplies: 5Last Post: 04-25-2011, 01:10 PM -
BufferedReader closing by itself (not what i want)
By Sneaky Fox in forum Threads and SynchronizationReplies: 2Last Post: 02-27-2011, 05:43 AM -
closing FileOutputStream?
By mgrootsch in forum New To JavaReplies: 1Last Post: 05-17-2010, 05:36 PM -
Can i use a resultset more than once without closing it
By haneeshrawther in forum JDBCReplies: 5Last Post: 06-26-2008, 03:16 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks