Java Reference
In-Depth Information
Example 10•28: ThemeManager.java
package com.davidflanagan.examples.gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.metal.DefaultMetalTheme;
/**
* This class reads theme descriptions from a GUIResourceBundle and uses them
* to specify colors and fonts for the Metal look-and-feel.
**/
public class ThemeManager {
JFrame frame;
// The frame which themes are applied to
GUIResourceBundle resources;
// Properties describing the themes
/**
* Build a ThemeManager for the frame and resource bundle. If there
* is a default theme specified, apply it to the frame
**/
public ThemeManager(JFrame frame, GUIResourceBundle resources) {
this.frame = frame;
this.resources = resources;
String defaultName = getDefaultThemeName();
if (defaultName != null) setTheme(defaultName);
}
/** Look up the named theme, and apply it to the frame */
public void setTheme(String themeName) {
// Look up the theme in the resource bundle
Theme theme = new Theme(resources, themeName);
// Make it the current theme
MetalLookAndFeel.setCurrentTheme(theme);
// Re-apply the Metal look-and-feel to install new theme
try { UIManager.setLookAndFeel(new MetalLookAndFeel()); }
catch(UnsupportedLookAndFeelException e) {}
// Propagate the new l&f across the entire component tree of the frame
SwingUtilities.updateComponentTreeUI(frame);
}
/** Get the "display name" or label of the named theme */
public String getDisplayName(String themeName) {
return resources.getString(themeName + ".name", null);
}
/** Get the name of the default theme, or null */
public String getDefaultThemeName() {
return resources.getString("defaultTheme", null);
}
/**
* Get the list of all known theme names. The returned values are
* theme property names, not theme display names.
**/
public String[] getAllThemeNames() {
java.util.List names = resources.getStringList("themelist");
return (String[]) names.toArray(new String[names.size()]);
}
Search WWH ::




Custom Search