Unsolicited About Boxes

Q: My Java 1.4 application has an ApplicationListener with a handleAbout method, but the default about box still appears over my own when "About MyApp" is selected from the Application menu. Why?

A: The problem here is that the ApplicationEvent you received in handleAbout needs to be set to the handled state; otherwise the eAWT will not know that you have indeed handled the request. This is stated in the eAWT javadocs, but commonly overlooked.

Below is a simplified example of a proper handleAbout method. Note that at the end of the method, a call to ApplicationEvent.setHandled(true) is made. Without this call, the system assumes that you have not actually handled the event and will put up its own default about box.

Listing 1: Suppressing the default aboutBox using setHandled(true).

public void handleAbout(ApplicationEvent e) {
   new AboutBox().show();
   // Tell eAWT you've handled the request, disabling the default about box.
   e.setHandled(true);
}

A similar problem may occur with the handleQuit method; in this case, you actually call ApplicationEvent.setHandled(true) to allow the application to quit. In other words, you have successfully handled the quit request and are ready for the default handler to continue.

Listing 2: Authorizing a quit using setHandled(true).

public void handleQuit(ApplicationEvent e) {
   cleanupResources();
   // The quit request is handled; proceed with app termination
   e.setHandled(true);
}

If you wish to have complete control over the quit and call System.exit yourself, for example, set the event's state to false in your handleQuit method. Note that although the default handled state of an ApplicationEvent is false, we still recommend setting the state explicitly to ensure the behavior you want.

Listing 3: Rejecting a quit using setHandled(false).

public void handleQuit(ApplicationEvent e) {
   // Reject the quit request and handle it as we see fit
   e.setHandled(false);
   String msg = "Are you sure you want to quit?"
   int result = JOptionPane.showConfirmDialog(mainFrame, msg);
   if (result == JOptionPane.YES_OPTION) {
      System.exit(0);
   }
}

Note: This Q&A refers to the eAWT APIs that were introduced with Java 1.4.1 on Mac OS X. For information on quit events in Java 1.3, please see Technical Q&A 1187.

Note: The about and quit events are the only eAWT events that require handling; other methods such as handlePrefs and handleOpenFile do not require any changes to the event state in order to be processed correctly.

Document Revision History

DateNotes
2004-08-31Formatting changes in code listings
2004-08-25How to properly override the default about and quit behaviors in Java 1.4 and later

Posted: 2004-08-31


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.