Java Reference
In-Depth Information
Solution
There are a variety of small steps you can take to improve your GUI's appearance and beha-
vior under Mac OS X.
Discussion
Although 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 de-
fault is the full class name of your main application class).
But there is no point in 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:
boolean isMacOS = System.getProperty("mrj.version") != null;
if (isMacOS) {
System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("com.apple.mrj.application.apple.menu.about.name",
"JabaDex");
}
Or, because these properties are likely harmless on non-Mac systems, you could just skip the
test, and set the two properties unconditionally, as is done in
Example 14-11
.
Example 14-11. MacOsUiHints.java
/**
* Interactive test for "macosui" package.
* Class cannot extend JFrame; must set properties before first
* call to any Swing constructor.
*/
public
public class
class
MacOsUiHints
MacOsUiHints
{
public
public static
static
void
void
main
(
String
[]
args
)
throws
throws
Exception
{
// OS X Tester: