Closing an Application
Posted by Rob Camick on May 1, 2009
I’m sure the users are having a great time using your application but as we all know, “all good things must come to an end“, so at some time the user is going to close the application. The user will generally have three ways to close an application:
- click on the “Close” button of the frame
- select the “Close” item from the sytem menu
- select the “Exit” item that is typically found in the “File” menu of the application
You may have some application specific “close processing” such as:
- prompting the user to confirm closing of the application
- saving application properties to be used the next time the application starts
So how do you ensure that close processing is done no matter how the application is closed?
Well, we need to understand what happens when a user attempts to close a frame. First, any WindowListener that has been added to the frame will be invoked. In particular the windowClosing event will be fired to indicate the intention to close the frame. Secondly, the JFrame will handle the close request based on the property set by invoking the setDefaultCloseOperation(…) method of the frame. The values of this property are:
- DO_NOTHING_ON_CLOSE – Don’t do anything
- HIDE_ON_CLOSE (default) – Automatically hide the frame
- DISPOSE_ON_CLOSE – Automatically hide and dispose the frame. When this is the only frame in the application the VM will also be terminated.
- EXIT_ON_CLOSE – Exit the application using the System.exit method (only valid on a JFrame, not a JDialog)
Therefore you need to manage the default close operation of the frame and implement some action for the windowClosing event of the WindowListener. A simple implementation that prompts the user to confirm the closing of the frame might be something like:
frame.addWindowListener( new WindowAdapter()
public void windowClosing(WindowEvent e)
JFrame frame = (JFrame)e.getSource();
int result = JOptionPane.showConfirmDialog(
"Are you sure you want to exit the application?",
if (result == JOptionPane.YES_OPTION)
Or, to make this process easier, so you don’t have to write a custom WindowListener for every application, you can use the CloseListener class. It provides support for the following functions:
- displaying a dialog asking the user to confirm application closing
- invoking a “Close Action” to perform some final processing before the frame is closed. The close action is only invoked if the application closing is confirmed.
- setting the default close operation to the appropriate value. The default would be to use “exit”. This can be changed to “dispose” by using the setDisposeOnClose() method. If the application closing is not confirmed then it will be set to “do nothing”.
You can create the CloseListener class using 3 different constructors:
- with a close message
- with a close action
- with a close message and a close action
To create a CloseListener that displays a close message in a confirm dialog you would use code like the following:
CloseListener cl = new CloseListener( "Are you sure you want to exit the application", "Exit Application"); JFrame frame = new JFrame("Closing an Application"); frame.addWindowListener( cl );
When the application is closed, you will see the following:
Obviously, the close action will be different for most applications so I’ll let you experiment with writing your own. All you need to do is extend AbstractAction and implement the actionPerformed() method with your custom code.
At the start I said there where three general ways to close an application. I’ve only discussed two of these above. So I’m sure you can’t wait any longer to hear about using an “Exit” menu item to close the application. Well, this is not part of a frame and must be added to any JMenuBar and JMenu you create for your application. To handle this you can use the ExitAction class. You simply create this class and then add it to a JMenuItem. When the menu item is invoked the action generates a windowClosing event that is dispatched to the frame. Once the event is dispatched, processing is the same as described above.
The code to use the ExitAction would be something like:
JMenu menu = new JMenu( "File" );
menu.add( ... );
menu.add( new ExitAction() );
Hopefully, using these two classes will make closing an application easier to implement. Although most of the discussion was geared towards a JFrame, the concepts still apply to a JDialog.