A Mac Java ApplicationAdapter class example

Before I get too far away from all of the Java/Swing/Mac GUI code I've been working on lately, I thought I'd share this Mac Java ApplicationAdapter class implementation.

This class is an example implementation of Apple's ApplicationAdapter class, which itself is a stub implementation of their own ApplicationListener interface. If you want to write Java GUI code on Mac OS X, it's important to learn about the ApplicationAdapter class, so you can handle the About, Preferences, and Quit events properly.

My Mac Java ApplicationAdapter example class

Without any further introduction, here's the source code for my example Mac/Java ApplicationAdapter class:

package com.devdaily.desktopshield;

import javax.swing.JOptionPane;
import com.apple.eawt.ApplicationAdapter;
import com.apple.eawt.ApplicationEvent;

/**
 * MyApplicationAdapter.java
 * Copyright 2010, Alvin J. Alexander, devdaily.com.
 * 
 * This file is part of the DesktopShield application. This class implements the 
 * Apple/Mac/Java ApplicationAdapter class, specifically the handleQuit
 * method of that class, to help shut down this application properly.
 *
 * The DesktopShield application is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * The DesktopShield application is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the DesktopShield application. If not, see <http://www.gnu.org/licenses/>.
*/
public class MyApplicationAdapter extends ApplicationAdapter
{
  private DesktopShield handler;
  
  public MyApplicationAdapter(DesktopShield handler)
  {
    this.handler = handler;
  }

  public void handleQuit(ApplicationEvent e)
  {
    System.exit(0);
  }

  public void handleAbout(ApplicationEvent e)
  {
    // tell the system we're handling this, so it won't display
    // the default system "about" dialog after ours is shown.
    e.setHandled(true);
    JOptionPane.showMessageDialog(null, "Show About dialog here");
  }

  public void handlePreferences(ApplicationEvent e)
  {
    JOptionPane.showMessageDialog(null, "Show Preferences dialog here");
  }
}

As you can see, this class extends the Apple ApplicationAdapter class, and specifically implements the following methods, which go back to the ApplicationListener interface:

  • handleQuit
  • handleAbout
  • handlePreferences

I'm not going to discuss those methods in this article, but you can find much more information about them in the tutorials I've linked to below.

More "Mac Java" programming tutorials

My only purpose for this article is to share this Java/Mac ApplicationAdapter/ApplicationListener example class, but if you're interested in more information on Java/Mac programming, here are several links with much more information: