Themes and the Metal Look-and-Feel

The default platform-independent look-and-feel for Swing applications is known as the Metal look-and-feel. One of the powerful but little-known features of Metal is that the fonts and colors it uses are easily customizable. All you have to do is pass a MetalTheme object to the static setCurrentTheme( ) method of MetalLookAndFeel. (These classes are defined in the infrequently used javax.swing.plaf.metal package.)

The MetalTheme class is abstract, so, in practice, you work with DefaultMetalTheme. This class has six methods that return the basic theme colors (really three shades each of a primary and a secondary color) and four methods that return the basic theme fonts. To define a new theme, all you have to do is subclass DefaultMetalTheme and override these methods to return the fonts and colors you want. (If you want more customizability than this, you have to subclass MetalTheme directly.)

Example 11-28 is a listing of ThemeManager.java. This example includes a subclass of DefaultMetalTheme, but defines it as an inner class of ThemeManager. The ThemeManager class provides the ability to read theme definitions (i.e., color and font specifications) from a GUIResourceBundle and defines methods for reading the name of a default theme and a list of names of all available themes from the bundle. Finally, ThemeManager can return a JMenu component that displays a list of available themes to the user and switches the current theme based ...

Get Java Examples in a Nutshell, 3rd Edition 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.