Java Reference
In-Depth Information
ThemeManager
, and the
JMenu
component it creates, were used in the
WebBrowser
class of Example 10-21. Before you examine the
ThemeManager
code, take a look at
the following lines excerpted from the
WebBrowserResources.properties
file,
which define the set of available themes for the web browser:
# This property defines the property names of all available themes.
themelist: theme.metal theme.rose, theme.lime, theme.primary, theme.bigfont
# This property defines the name of the default property
defaultTheme: theme.metal
# This theme only has a name. All font and color values are unchanged from
# the default Metal theme
theme.metal.name: Default Metal
# This theme uses shades of red/pink
theme.rose.name: Rose
theme.rose.primary: #905050
theme.rose.secondary: #906050
# This theme uses lime green colors
theme.lime.name: Lime
theme.lime.primary: #509050
theme.lime.secondary: #506060
# This theme uses bright primary colors
theme.primary.name: Primary Colors
theme.primary.primary: #202090
theme.primary.secondary: #209020
# This theme uses big fonts and the default colors
theme.bigfont.name: Big Fonts
theme.bigfont.controlFont: sansserif-bold-18
theme.bigfont.menuFont: sansserif-bold-18
theme.bigfont.smallFont: sansserif-plain-14
theme.bigfont.systemFont: sansserif-plain-14
theme.bigfont.userFont: sansserif-plain-14
theme.bigfont.titleFont: sansserif-bold-18
With these theme definitions, you should have no trouble understanding the
resource parsing code of
ThemeManager
.
getThemeMenu()
creates a
JMenu
populated
by
JRadioButtonMenuItem
objects, rather than
JMenuItem
or
Action
objects, as
we've seen earlier in this chapter. This emphasizes the fact that only one theme
can be selected at a time. When the theme is changed, the
setTheme()
method
uses a
SwingUtilities
method to propagate the change to all components within
the frame. Finally, note that the
Theme
inner class doesn't use
Font
and
Color
objects, but
FontUIResource
and
ColorUIResource
objects instead. These classes
are part of the
javax.swing.plaf
package and are trivial subclasses of
Font
and
Color
that implement the
UIResource
marker interface. This interface allows com-
ponents to distinguish between property values assigned by the look-and-feel,
which all implement
UIResource
, and property values assigned by the application.
Based on this distinction, application settings can override look-and-feel settings,
even when the look-and-feel (or theme) changes while the application is running.