Java Tips Weblog

  • Blog Stats

    • 115,201 hits
  • Categories

  • Archives

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)

Thererfore, in order to take control of the frame closing process we will need to handle the windowClosing event so that the default close operation can be set to an appropriate value. To make this process easier 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 with a close message a close action or both. To create a CloseListener that displays 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:
 

closing-an-application

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.

Get The Code

CloseListener.java
ExitAction.java

Related Reading

Java Tutorials: How to Write a Window Listener
Java Tutorials: How to Use Actions
Java API: javax.swing.JFrame.setDefaultCloseOperation

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>