up previous next contents
Up: java-on-mac Previous: Converting Control keystrokes to Next: (New approach) Handling the   Contents

(Old approach) Handling the standard menu callbacks

Now that I have things looking a lot better, I need to also make them work better. The first thing to do is to handle the standard menu items on the application menu, i.e., the About, Preferences, and Quit menu items. For reference purposes, Figure 7.1 shows this menu for my WikiTeX application.

Figure 7.1: This is the application menu for my WikiTeX application.
Image 6-application-menu

There are two ways to handle this, the old way and the new way. I'll show you both techniques, but as the old way has been marked deprecated, you should use the new way, unless you're tied to an older platform.

The old approach

The first thing you need to do is create one or more classes to handle callbacks from these menu items. For the purposes of this example I've created one class that implements the necessary interfaces, specifically the MRJAboutHandler, MRJPrefsHandler, and MRJQuitHandler handlers, as shown below:

package com.devdaily.opensource.jelly.controller;

import javax.swing.JOptionPane;
import com.apple.mrj.MRJAboutHandler;
import com.apple.mrj.MRJPrefsHandler;
import com.apple.mrj.MRJQuitHandler;

public class MacOSXController 
implements MRJAboutHandler, MRJQuitHandler, MRJPrefsHandler
{
  public void handleAbout()
  {
    JOptionPane.showMessageDialog(null, 
                                  "about", 
                                  "about", 
                                  JOptionPane.INFORMATION_MESSAGE);
  }

  public void handlePrefs() throws IllegalStateException
  {
    JOptionPane.showMessageDialog(null, 
                                  "prefs", 
                                  "prefs", 
                                  JOptionPane.INFORMATION_MESSAGE);
  }

  public void handleQuit() throws IllegalStateException
  {
    JOptionPane.showMessageDialog(null, 
                                  "quit", 
                                  "quit", 
                                  JOptionPane.INFORMATION_MESSAGE);
    // handle exit here
    // System.exit(0);
  }

}

I'm not doing anything with these handlers, I'm just showing a dialog when each menu item is selected. Note that with the handleQuit method you're really going to want to exit your application after an "Are you sure?" prompt.

Once you have that class added to your project, you need to add the handlers somewhere during your initial setup. I did this with code like this:

MacOSXController macController = new MacOSXController();
if (IS_MAC)
{
  MRJApplicationUtils.registerAboutHandler(macController);
  MRJApplicationUtils.registerPrefsHandler(macController);
  MRJApplicationUtils.registerQuitHandler(macController);
}

I haven't tried this on other systems yet, but I've read that if you call the registerPrefsHandler on non-Mac systems you'll have some issues, so I just put all the method calls inside the if clause.

When I run this code on my Mac system and select the About, Preferences, and Quit menu items, I do see that each of my dialogs is displayed, so I'll call this working.

The bad news about this approach is that it's deprecated, and there's a new approach for handling these items in the future. I'll cover that next.