Recipe 14.16 Enhancing Your GUI for Mac OS X


You tried running your Swing GUI application on Mac OS X, and it didn't look right.


There are a variety of small steps you can take to improve your GUI's appearance and behavior under Mac OS X.


While Swing aims to be a portable GUI, Apple's implementation for Mac OS X does not automatically do "the right thing" for everyone. For example, a JMenuBar menu container appears by default at the top of the application window. This is the norm on Windows and on most Unix platforms, but Mac users expect the menu bar for the active application to appear at the top of the screen. To enable "normal" behavior, you have to set the System property apple.laf.useScreenMenuBar to the value true, as in java -Dapple.laf.useScreenMenuBar=true SomeClassName. You might want to set some other properties too, such as a short name for your application to appear in the menu bar (the default is the full class name of your main application class).

But there is no point setting these properties unless you are, in fact, running on Mac OS X. How do you tell? Apple's recommended way is to check for the system property mrj.runtime and, if so, assume you are on Mac OS X.[3] So the code might be something like this:

[3] In fact, you could be running on the ancient (JDK 1.1.8) MRJ under Mac OS 8 or 9, but that's statistically very unlikely.

boolean isMacOS = System.getProperty("mrj.version") != null; if (isMacOS) {   System.setProperty("apple.laf.useScreenMenuBar",  "true");   System.setProperty("", "JabaDex"); }

But there's more! You still don't get to handle "normal" Mac-style Quit, Preferences, Print or About requests (Command-Q, Command-comma, Command-P, or Application About, respectively). For these you may need to use some classes in the package. You can read about this in the Apple Developer Documentation that comes with Mac OS X. Or you can just use my adapter class, com.darwinsys.macosui.MacOSAppAdapter . You need to implement some of my four interfaces (in the same package): AboutBoxHandler, PrefsHandler, PrintHandler, and ShutdownHandler. Each of these has one method that is invoked when the Mac user invokes the relevant request. Sample code that implements this is in Example 14-9; this program doubles as a primitive interactive test, and it can be invoked by ant regress.macosui in the darwinsys directory of the online source code.

As of build 1.4.2, the Print dialog is not invoked. Also, you (obviously) can neither compile nor run any program using this package against a copy of the darwinsys.jar file that was compiled on a non-Mac OS X platform, such as Windows or Unix.

Example 14-9.
/**  * Interactive test for "macosui" package.  * Class can not extend JFrame; must invoke setMacOS( ) before first  * call to any Swing constructor.  */ public class MacOSUITest {     public static void main(String[] args) {         // Tester: check that this string appears in the Application Menu.         MacOSUtil.setMacOS("MacOSUITest");         new MacOSUITest( );     }          public MacOSUITest( ) {         JFrame jf = new JFrame("MacOSUITest");         JButton button = new JButton("Exit");         button.addActionListener(new ActionListener( ) {             public void actionPerformed(ActionEvent arg0) {                 System.exit(0);             }         });         jf.getContentPane( ).add(button);         // Tester: see that Application->About produces our popup         // Ditto for Preferences and Shutdown.         MacOSAppAdapter adapter =             new MacOSAppAdapter(jf, abouter, prefser, printer, shutter);         adapter.register( );         jf.setSize(300, 200);         jf.setVisible(true);     }             // Definititions of individual action handlers omitted. }

See Also

See the O'Reilly book Mac OS X for Java Geeks by Will Iverson for more information on Mac OS X. Apple's web site includes several Technical Notes at

Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: