Java Reference
In-Depth Information
Example 10•27: MenuParser.java (continued)
case '>': // words beginning with > are submenu names
// strip off the > character, and recurse to parse the submenu
item = item.substring(1);
// Parse a submenu and add it to the list of items
JMenu submenu = (JMenu)parse(bundle, item, JMenu.class);
if (menu != null) menu.add(submenu);
else popup.add(submenu);
break;
case '!': // words beginning with ! are action names
item = item.substring(1); // strip off the ! character
/* falls through */ // fall through to the next case
default: // By default all other words are taken as action names
// Look up the named action and add it to the menu
Action action = (Action)bundle.getResource(item, Action.class);
if (menu != null) menu.add(action);
else popup.add(action);
break;
}
}
// Finally, return the menu or the popup menu
if (menu != null) return menu;
else return popup;
}
}
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 Metal-
LookAndFeel .
(These classes are
defined in the infrequently used
javax.swing.plaf.metal package.)
The MetalTheme class is abstract, so, in practice, you work with DefaultMet-
alTheme . 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 Default-
MetalTheme 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 10-28 is a listing of ThemeManager.java . This example includes a sub-
class 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 . It also 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 returna JMenu component that displays a list of
available themes to the user and switches the current theme based on the user's
selection.
Search WWH ::




Custom Search