Home » openjdk-7 » javax » swing » [javadoc | source]
javax.swing
public class: UIManager [javadoc | source]
java.lang.Object
   javax.swing.UIManager

All Implemented Interfaces:
    java$io$Serializable

{@code UIManager} manages the current look and feel, the set of available look and feels, {@code PropertyChangeListeners} that are notified when the look and feel changes, look and feel defaults, and convenience methods for obtaining various default values.

Specifying the look and feel

The look and feel can be specified in two distinct ways: by specifying the fully qualified name of the class for the look and feel, or by creating an instance of {@code LookAndFeel} and passing it to {@code setLookAndFeel}. The following example illustrates setting the look and feel to the system look and feel:
  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
The following example illustrates setting the look and feel based on class name:
  UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
Once the look and feel has been changed it is imperative to invoke {@code updateUI} on all {@code JComponents}. The method SwingUtilities#updateComponentTreeUI makes it easy to apply {@code updateUI} to a containment hierarchy. Refer to it for details. The exact behavior of not invoking {@code updateUI} after changing the look and feel is unspecified. It is very possible to receive unexpected exceptions, painting problems, or worse.

Default look and feel

The class used for the default look and feel is chosen in the following manner:
  1. If the system property swing.defaultlaf is {@code non-null}, use its value as the default look and feel class name.
  2. If the java.util.Properties file swing.properties exists and contains the key swing.defaultlaf, use its value as the default look and feel class name. The location that is checked for swing.properties may vary depending upon the implementation of the Java platform. In Sun's implementation the location is ${java.home}/lib/swing.properties. Refer to the release notes of the implementation being used for further details.
  3. Otherwise use the cross platform look and feel.

Defaults

{@code UIManager} manages three sets of {@code UIDefaults}. In order, they are:
  1. Developer defaults. With few exceptions Swing does not alter the developer defaults; these are intended to be modified and used by the developer.
  2. Look and feel defaults. The look and feel defaults are supplied by the look and feel at the time it is installed as the current look and feel ({@code setLookAndFeel()} is invoked). The look and feel defaults can be obtained using the {@code getLookAndFeelDefaults()} method.
  3. Sytem defaults. The system defaults are provided by Swing.
Invoking any of the various {@code get} methods results in checking each of the defaults, in order, returning the first {@code non-null} value. For example, invoking {@code UIManager.getString("Table.foreground")} results in first checking developer defaults. If the developer defaults contain a value for {@code "Table.foreground"} it is returned, otherwise the look and feel defaults are checked, followed by the system defaults.

It's important to note that {@code getDefaults} returns a custom instance of {@code UIDefaults} with this resolution logic built into it. For example, {@code UIManager.getDefaults().getString("Table.foreground")} is equivalent to {@code UIManager.getString("Table.foreground")}. Both resolve using the algorithm just described. In many places the documentation uses the word defaults to refer to the custom instance of {@code UIDefaults} with the resolution logic as previously described.

When the look and feel is changed, {@code UIManager} alters only the look and feel defaults; the developer and system defaults are not altered by the {@code UIManager} in any way.

The set of defaults a particular look and feel supports is defined and documented by that look and feel. In addition, each look and feel, or {@code ComponentUI} provided by a look and feel, may access the defaults at different times in their life cycle. Some look and feels may agressively look up defaults, so that changing a default may not have an effect after installing the look and feel. Other look and feels may lazily access defaults so that a change to the defaults may effect an existing look and feel. Finally, other look and feels might not configure themselves from the defaults table in any way. None-the-less it is usually the case that a look and feel expects certain defaults, so that in general a {@code ComponentUI} provided by one look and feel will not work with another look and feel.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see java.beans.XMLEncoder .

Nested Class Summary:
public static class  UIManager.LookAndFeelInfo  Provides a little information about an installed LookAndFeel for the sake of configuring a menu or for initial application set up. 
Method from javax.swing.UIManager Summary:
addAuxiliaryLookAndFeel,   addPropertyChangeListener,   get,   get,   getAuxiliaryLookAndFeels,   getBoolean,   getBoolean,   getBorder,   getBorder,   getColor,   getColor,   getCrossPlatformLookAndFeelClassName,   getDefaults,   getDimension,   getDimension,   getFont,   getFont,   getIcon,   getIcon,   getInsets,   getInsets,   getInstalledLookAndFeels,   getInt,   getInt,   getLookAndFeel,   getLookAndFeelDefaults,   getPropertyChangeListeners,   getString,   getString,   getString,   getSystemLookAndFeelClassName,   getUI,   installLookAndFeel,   installLookAndFeel,   put,   removeAuxiliaryLookAndFeel,   removePropertyChangeListener,   setInstalledLookAndFeels,   setLookAndFeel,   setLookAndFeel
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from javax.swing.UIManager Detail:
 public static  void addAuxiliaryLookAndFeel(LookAndFeel laf) 
    Adds a LookAndFeel to the list of auxiliary look and feels. The auxiliary look and feels tell the multiplexing look and feel what other LookAndFeel classes for a component instance are to be used in addition to the default LookAndFeel class when creating a multiplexing UI. The change will only take effect when a new UI class is created or when the default look and feel is changed on a component instance.

    Note these are not the same as the installed look and feels.

 public static  void addPropertyChangeListener(PropertyChangeListener listener) 
    Adds a PropertyChangeListener to the listener list. The listener is registered for all properties.
 public static Object get(Object key) 
    Returns an object from the defaults.
 public static Object get(Object key,
    Locale l) 
    Returns an object from the defaults that is appropriate for the given locale.
 public static LookAndFeel[] getAuxiliaryLookAndFeels() 
    Returns the list of auxiliary look and feels (can be null). The auxiliary look and feels tell the multiplexing look and feel what other LookAndFeel classes for a component instance are to be used in addition to the default LookAndFeel class when creating a multiplexing UI.

    Note these are not the same as the installed look and feels.

 public static boolean getBoolean(Object key) 
    Returns a boolean from the defaults which is associated with the key value. If the key is not found or the key doesn't represent a boolean value then {@code false} is returned.
 public static boolean getBoolean(Object key,
    Locale l) 
    Returns a boolean from the defaults which is associated with the key value and the given Locale. If the key is not found or the key doesn't represent a boolean value then {@code false} will be returned.
 public static Border getBorder(Object key) 
    Returns a border from the defaults. If the value for {@code key} is not a {@code Border}, {@code null} is returned.
 public static Border getBorder(Object key,
    Locale l) 
    Returns a border from the defaults that is appropriate for the given locale. If the value for {@code key} is not a {@code Border}, {@code null} is returned.
 public static Color getColor(Object key) 
    Returns a color from the defaults. If the value for {@code key} is not a {@code Color}, {@code null} is returned.
 public static Color getColor(Object key,
    Locale l) 
    Returns a color from the defaults that is appropriate for the given locale. If the value for {@code key} is not a {@code Color}, {@code null} is returned.
 public static String getCrossPlatformLookAndFeelClassName() 
    Returns the name of the LookAndFeel class that implements the default cross platform look and feel -- the Java Look and Feel (JLF). This value can be overriden by setting the swing.crossplatformlaf system property.
 public static UIDefaults getDefaults() 
    Returns the defaults. The returned defaults resolve using the logic specified in the class documentation.
 public static Dimension getDimension(Object key) 
    Returns a dimension from the defaults. If the value for {@code key} is not a {@code Dimension}, {@code null} is returned.
 public static Dimension getDimension(Object key,
    Locale l) 
    Returns a dimension from the defaults that is appropriate for the given locale. If the value for {@code key} is not a {@code Dimension}, {@code null} is returned.
 public static Font getFont(Object key) 
    Returns a font from the defaults. If the value for {@code key} is not a {@code Font}, {@code null} is returned.
 public static Font getFont(Object key,
    Locale l) 
    Returns a font from the defaults that is appropriate for the given locale. If the value for {@code key} is not a {@code Font}, {@code null} is returned.
 public static Icon getIcon(Object key) 
    Returns an Icon from the defaults. If the value for {@code key} is not an {@code Icon}, {@code null} is returned.
 public static Icon getIcon(Object key,
    Locale l) 
    Returns an Icon from the defaults that is appropriate for the given locale. If the value for {@code key} is not an {@code Icon}, {@code null} is returned.
 public static Insets getInsets(Object key) 
    Returns an Insets object from the defaults. If the value for {@code key} is not an {@code Insets}, {@code null} is returned.
 public static Insets getInsets(Object key,
    Locale l) 
    Returns an Insets object from the defaults that is appropriate for the given locale. If the value for {@code key} is not an {@code Insets}, {@code null} is returned.
 public static LookAndFeelInfo[] getInstalledLookAndFeels() 
    Returns an array of {@code LookAndFeelInfo}s representing the {@code LookAndFeel} implementations currently available. The LookAndFeelInfo objects can be used by an application to construct a menu of look and feel options for the user, or to determine which look and feel to set at startup time. To avoid the penalty of creating numerous {@code LookAndFeel} objects, {@code LookAndFeelInfo} maintains the class name of the {@code LookAndFeel} class, not the actual {@code LookAndFeel} instance.

    The following example illustrates setting the current look and feel from an instance of {@code LookAndFeelInfo}:

      UIManager.setLookAndFeel(info.getClassName());
    
 public static int getInt(Object key) 
    Returns an integer from the defaults. If the value for {@code key} is not an {@code Integer}, or does not exist, {@code 0} is returned.
 public static int getInt(Object key,
    Locale l) 
    Returns an integer from the defaults that is appropriate for the given locale. If the value for {@code key} is not an {@code Integer}, or does not exist, {@code 0} is returned.
 public static LookAndFeel getLookAndFeel() 
    Returns the current look and feel or null.
 public static UIDefaults getLookAndFeelDefaults() 
    Returns the {@code UIDefaults} from the current look and feel, that were obtained at the time the look and feel was installed.

    In general, developers should use the {@code UIDefaults} returned from {@code getDefaults()}. As the current look and feel may expect certain values to exist, altering the {@code UIDefaults} returned from this method could have unexpected results.

 public static PropertyChangeListener[] getPropertyChangeListeners() 
    Returns an array of all the PropertyChangeListeners added to this UIManager with addPropertyChangeListener().
 public static String getString(Object key) 
    Returns a string from the defaults. If the value for {@code key} is not a {@code String}, {@code null} is returned.
 public static String getString(Object key,
    Locale l) 
    Returns a string from the defaults that is appropriate for the given locale. If the value for {@code key} is not a {@code String}, {@code null} is returned.
 static String getString(Object key,
    Component c) 
    Returns a string from the defaults that is appropriate for the given locale. If the value for {@code key} is not a {@code String}, {@code null} is returned.
 public static String getSystemLookAndFeelClassName() 
    Returns the name of the LookAndFeel class that implements the native system look and feel if there is one, otherwise the name of the default cross platform LookAndFeel class. This value can be overriden by setting the swing.systemlaf system property.
 public static ComponentUI getUI(JComponent target) 
    Returns the appropriate {@code ComponentUI} implementation for {@code target}. Typically, this is a cover for {@code getDefaults().getUI(target)}. However, if an auxiliary look and feel has been installed, this first invokes {@code getUI(target)} on the multiplexing look and feel's defaults, and returns that value if it is {@code non-null}.
 public static  void installLookAndFeel(LookAndFeelInfo info) 
    Adds the specified look and feel to the set of available look and feels. While this method allows a {@code null} {@code info}, it is strongly recommended that a {@code non-null} value be used.
 public static  void installLookAndFeel(String name,
    String className) 
    Adds the specified look and feel to the set of available look and feels. While this method does not check the arguments in any way, it is strongly recommended that {@code non-null} values be supplied.
 public static Object put(Object key,
    Object value) 
    Stores an object in the developer defaults. This is a cover method for {@code getDefaults().put(key, value)}. This only effects the developer defaults, not the system or look and feel defaults.
 public static boolean removeAuxiliaryLookAndFeel(LookAndFeel laf) 
    Removes a LookAndFeel from the list of auxiliary look and feels. The auxiliary look and feels tell the multiplexing look and feel what other LookAndFeel classes for a component instance are to be used in addition to the default LookAndFeel class when creating a multiplexing UI. The change will only take effect when a new UI class is created or when the default look and feel is changed on a component instance.

    Note these are not the same as the installed look and feels.

 public static  void removePropertyChangeListener(PropertyChangeListener listener) 
    Removes a PropertyChangeListener from the listener list. This removes a PropertyChangeListener that was registered for all properties.
 public static  void setInstalledLookAndFeels(LookAndFeelInfo[] infos) throws SecurityException 
    Sets the set of available look and feels. While this method does not check to ensure all of the {@code LookAndFeelInfos} are {@code non-null}, it is strongly recommended that only {@code non-null} values are supplied in the {@code infos} array.
 public static  void setLookAndFeel(LookAndFeel newLookAndFeel) throws UnsupportedLookAndFeelException 
    Sets the current look and feel to {@code newLookAndFeel}. If the current look and feel is {@code non-null} {@code uninitialize} is invoked on it. If {@code newLookAndFeel} is {@code non-null}, {@code initialize} is invoked on it followed by {@code getDefaults}. The defaults returned from {@code newLookAndFeel.getDefaults()} replace those of the defaults from the previous look and feel. If the {@code newLookAndFeel} is {@code null}, the look and feel defaults are set to {@code null}.

    A value of {@code null} can be used to set the look and feel to {@code null}. As the {@code LookAndFeel} is required for most of Swing to function, setting the {@code LookAndFeel} to {@code null} is strongly discouraged.

    This is a JavaBeans bound property.

 public static  void setLookAndFeel(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException 
    Loads the {@code LookAndFeel} specified by the given class name, using the current thread's context class loader, and passes it to {@code setLookAndFeel(LookAndFeel)}.