Make Quick Look and Feel Changes #86
Chapter 11, Native Integration and Packaging
|
437
HACK
Some components even have borders. Menu items have borders but they are
turned off by default. You need to set an extra boolean to make a new bor-
der show up, as in Figure 11-14.
Border border = BorderFactory.createEtchedBorder(
EtchedBorder.LOWERED);
UIManager.put("MenuItem.border", border);
UIManager.put("MenuItem.borderPainted", new Boolean(true));
Use System Colors
The UIManager lets you change any color in your application to whatever you
want. Now, what if you wanted to create a theme that matched the native
operating system closer? You could hardcode some color values for each OS,
but most windowing systems also let their users customize their colors.
Hardcoded values wouldn’t take those dynamic colors into account. Fortu-
nately, AWT provides a way out: the
SystemColor class.
Figure 11-13. A JTextField with large insets
Figure 11-14. Menu items with etched borders
438
|
Chapter 11, Native Integration and Packaging
#86 Make Quick Look and Feel Changes
HACK
SystemColor is a special subclass of Color that provides access to most of the
standard color settings of any operating system. It also has the special abil-
ity to update itself whenever the underlying system color changes. This
means if the user switches her native colors from a control panel, your appli-
cation will automatically update itself to reflect the new settings.
Instead of methods,
SystemColor
has a bunch of constants that define each
type of color from
activeCaptionText to windowBorder. The documentation is
minor, so you will need to play around with different settings to get the
effect you are looking for:
Color sysbg = SystemColor.control;
Color sysfg = SystemColor.controlText;
UIManager.put("Button.background",sysbg);
UIManager.put("Button.foreground",sysfg);
This code sets the background and foreground of every button to use the
control and controlText fields of SystemColor. With my computer set to use
the Desert theme, it looks like Figure 11-15.
If I change my window theme to high-contrast black, it looks like
Figure 11-16.
UIManager properties give developers a simple way to customize the colors,
fonts, and borders of almost any Swing component. As an enhancement,
you could allow users to customize the colors themselves and store the val-
ues in a properties file.
Figure 11-15. Window with the Desert theme

Get Swing Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.