O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

414
|
Chapter 11, Native Integration and Packaging
#81 Make Mac Applications Behave Normally
HACK
To smooth over the cross-platform differences somewhat, Apple does cer-
tain things differently in its Java implementation. For one thing, it will auto-
matically put a Swing application into its native Look and Feel, Aqua, rather
than defaulting into cross-platform Metal or Ocean as would happen on
other platforms. In other words, you don’t have to do anything special to
pick up the Mac Look and Feel, although redundantly asking for and set-
ting the native Look and Feel classname doesn’t hurt either.
Moreover, Apple provides some key/value pairs that you can set in the Java
system properties to get even more Mac-like behavior. Because these proper-
ties all start with
apple or com.apple, you can set them and not worry that
they’ll affect the behavior of your application on any other platform.
Using the Apple System Properties
Apple has been changing the names and behaviors of these system proper-
ties for a while, and some of them are deprecated or no-op’ed, so I’ll just
show four of the most useful ones here. To see the whole list, check out the
Runtime System Properties of Apple’s Java 1.4.1 release notes on http://
developer.apple.com/releasenotes/Java/index.html.
You can set the properties several ways. The obvious way is to use the
-D
command-line argument:
java -Dapple.awt.showGrowBox=true MyClass
However, this becomes tedious to type after you decide to use multiple
properties. Another option is to simply call
System.setProperty( ) in your
code, though you’ll need to do so as soon as possible so the desired prop-
erty gets picked up by the JVM before it’s needed. A third option exists as
well: if you’re bundling the Java application as a double-clickable Mac appli-
cation, your tool of choice will give you an opportunity to set these values.
For example, the JarBundler that Apple provides with its developer tools has
a properties pane in which you can enter name/value pairs; these are saved
with the application bundle and provided to the JVM as if they’d been set on
the command line.
The downloadable book code contains demo applications
for all the tricks shown in this chapter, but since the point in
this hack is the one-line
System.setProperty( ) call and not
20 lines of code that demonstrate the effect, the demo code
is not shown here, just screenshots of the results.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required