up previous next contents
Up: java-on-mac Previous: (New approach) Handling the Next: A potpourri of other   Contents

Slightly different menus for the Mac

The last thing I need to do to make my menu bar very happy and Mac-compatible actually requires a few more code changes than anything I've shown before.

The biggest problem is that, on the Mac, it's not standard to have an Exit menu item under the File menu (see Figure 9.1). If you're a Mac user, you know this is normally handled by the Quit menu item on the Application menu. Beyond that, I also have a "Help | About" menu that should also replaced by the About menu item on the Application menu.

Figure 9.1: The "File | Exit" menu is not standard on Mac OS X.
Image 7-file-exit

There are a number of ways to make this fix. I'm going to show a bit of a brute-force method, and let you handle your situation a little more elegantly. (As an example of a more elegant approach, you could have one Menu class for Mac OS X, and a slightly different Menu class for Windows, and then use the right one for each platform.)

As I showed earlier in this tutorial, I have a boolean field named IS_MAC that tells me application where it's running on a Mac. Therefore, I can just wrap my Java code that sets up the File | Exit menu item in with a simple test, like this:

if (!IS_MAC) {
  // set up File | Exit here for other platforms
}

As I mentioned that's a little brute force, and there are more elegant ways to handle this, but for the purposes of this tutorial I think that gets the point across.

Another minor item is that the "File | New" menu item on a Mac is typically shown on Windows applications as "File | New ...", so this is another thing to look out for. This one is pretty minor, so you can ignore it, handle that with this approach, or handle it with a more elegant approach.

Note: This article on macdevcenter.com shows the beginnings of a more elegant coding approach in the section titled "Cleaning Up the Loose Ends."

Other mnemonics

In regards to accelerator keys, you may find that when you convert your [Control] keystrokes to OS X [Command] keystrokes, they might collide with the standard system meaning for that keystroke on the Mac OS X platform.

For instance, when I used my WikiTeX application on Windows, I used [Control][m] to bring up a context-sensitive popup menu. Well, [Command][m] on Mac OS X should actually minimize my application. So this is a case where I should choose a different mnemonic character to bring up my popup menu, something like [Command][p], for instance ("p" for "popup").

Again, I think the proper approach here is to handle your mnemonics by defining them for each platform, then using the Factory pattern to return the right set of mnemonics for the current platform. That's a little beyond the scope of this article, however, so I'm going to leave that as "an exercise for the reader."