Java and Mac OS X (Developer Reference #18) by T. Gene Davis

Java and Mac OS X (Developer Reference #18) by T. Gene Davis

Author:T. Gene Davis
Language: eng
Format: epub
Publisher: Wiley
Published: 2010-03-24T16:00:00+00:00


I use the FileManager class near the end of the openDialog() method. I call both getFileCreator() and getFileType(). Both static methods return int values. I display those two values in the message dialog box.

Investigating com.apple.eawt Classes

Most applications need a Help Viewer, a customized Dock menu, or at least a working preference menu item in the application menu. Intercepting system quit events, implementing an application menu preference item, and providing menu item searching from a Help menu on your screen menu may not “wow” your customers, but your OS X customers will notice if those features are missing. The com.apple.eawt package contains the classes for all these tasks.

Manipulating the Dock

One of the most obvious features of OS X is the Dock. When magnification is turned on, beautiful icons seem to burst into view as users mouse over them. Properly bundled Java applications take full advantage of the Dock and Dock menus.

The com.apple.eawt.Application class provides several methods for manipulating the OS X Dock. The Application's setDockIconImage() method allows you to customize your Dock icon beyond displaying your default application bundle icon. Also, the Application's setDockIconBadge() method enables system badging of the Dock icon. (In this context, badges are small images attached to an application's Dock icon that provide visual clues as to the current state of the application.)

The com.apple.eawtApplication method most commonly used by Java developers is the setDockMenu() method. This method places custom menu items and submenus on the dock.

Follow these steps to customize the Dock menu:

1. Instantiate your application's JFrame.

2. Create a java.awt.PopupMenu for use in the Dock.

3. Add menu items and submenus to the PopupMenu.

4. Add your PopupMenu to your JFrame with the add() method.

5. Instantiate a com.apple.eawt.Application object.

6. Add your PopupMenu to your Application object using the setDockMenu() method.

Tip

I encourage Java application bundling on OS X. However, if you want to forgo the joys of creating application bundles for your Java applications and still want to have a nice-looking name and icon in the OS X Dock, I have a couple of command line arguments for you.

The –Xdock:name=<some name> argument customizes your application name in the Dock, and the –Xdock:icon=<some path> argument customizes your Dock icon. For example,

java -Xdock:name=”My App” -cp ame.jar ¬

com.genedavis.ApplicationMenuExample

sets com.genedavis.ApplicationMenuExample to run under the name My App.

Tip

When running a bundled application in headless mode, meaning no menu or Dock icon, use a combination of the java.awt.headless property set to true and LSUIElement set to 1.

Some employers may not want a full application bundle created for their cross-platform Java application. Never fear, Dock modifications are available from double-clickable JAR files as well as bundled applications. The following example works well from the command line, a double-clickable JAR, and an application bundle.

The following source is for the DockExample class. DockExample follows the steps I stated for modifying the Dock menu with a PopupMenu. The DockExample class illustrates adding custom menus and menu items to the OS X Dock. The final Dock menu for the Dock Example program is shown in Figure 7.2. The source files for Dock Example are on the book's Web site.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.