-
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?
-
Re: closing a program
Does <CTL>Q cause a WindowEvent that your listener sees?
-
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.
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);
}
}
});
}
}
-
Re: closing a program
-
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?
-
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?
-
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.
-
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.
-
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. :(
-
Re: closing a program
There's a suggestion here that might help.
db
-
Re: closing a program
I'll look into it.
Thanks.
-
Re: closing a program
Thanks a lot, Darryl. That did the trick.