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

Create an Inverse Black-and-White Theme #87
Chapter 11, Native Integration and Packaging
|
439
HACK
H A C K
#87
Create an Inverse Black-and-White Theme Hack #87
Create a custom black-and-white theme for monochrome LCD displays using
a few simple
UIManager calls.
The UIManager lets you set simple resources for color, fonts, and padding,
but you may have noticed that if you want to theme all of the components,
you need to set properties on each one. Since Swing has over 300 Look and
Feel properties, this could become a problem. Fortunately, there is another
way to make global changes without creating an entire custom L&F. Metal,
the standard cross-platform L&F that comes with Swing, can use themes.
This hack demonstrates how to create a Metal theme that forces the compo-
nents to use only black and white.
Swing lets you switch between different Look and Feels. You can use a
native Look and Feel (such as the one that comes with Mac OS X) or a third-
party Look and Feel if you have them installed. Swing also comes with a
standard L&F called Metal. Metal is built into the JRE and is always avail-
able, making it the ideal L&F to customize.
Like all L&Fs, Metal has many, many classes that you can subclass to make
changes. However, it also comes with an interface called
MetalTheme. If you
implement
MetalTheme, then you can customize most of Metal without dig-
ging into the details. Most of the colors in Metal can be set, in fact, with just
six values. Metal makes it even easier by providing a default implementa-
tion called, unsurprisingly,
DefaultMetalTheme that you can use as a starting
point.
Figure 11-16. Window with the high-contrast black theme

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