javax.swing
Class UIManager

java.lang.Object
  extended byjavax.swing.UIManager
All Implemented Interfaces:
Serializable

public class UIManager
extends Object
implements Serializable

This class keeps track of the current look and feel and its defaults. The default look and feel class is chosen in the following manner:

  1. If the system property swing.defaultlaf is non-null, use it as the default look and feel class name.
  2. If the Properties file swing.properties exists and contains the key swing.defaultlaf, use its value as default look and feel class name. The location of swing.properties may vary depending upon the implementation of the Java platform. In Sun's implementation this will reside in &java.home>/lib/swing.properties. Refer to the release notes of the implementation you are using for further details.
  3. Otherwise use the Java look and feel.

We manage three levels of defaults: user defaults, look and feel defaults, system defaults. A call to UIManager.get checks all three levels in order and returns the first non-null value for a key, if any. A call to UIManager.put just affects the user defaults. Note that a call to setLookAndFeel doesn't affect the user defaults, it just replaces the middle defaults "level".

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 XMLEncoder.

Author:
Thomas Ball, Hans Muller
See Also:
javax.swing.plaf.metal, Serialized Form

Nested Class Summary
private static class UIManager.LAFState
          This class defines the state managed by the UIManager.
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.
 
Field Summary
private static String auxiliaryLAFsKey
           
private static Object classLock
           
private static UIManager.LAFState currentLAFState
           
private static Thread currentLAFStateThread
           
private static String defaultLAFKey
           
private static String disableMnemonicKey
           
private static UIManager.LookAndFeelInfo[] installedLAFs
          The default value of installedLAFS is used when no swing.properties file is available or if the file doesn't contain a "swing.installedlafs" property.
private static String installedLAFsKey
           
private static Object lafStateACKey
          The AppContext key for our one LAFState instance.
private static String multiplexingLAFKey
           
 
Constructor Summary
UIManager()
           
 
Method Summary
static void addAuxiliaryLookAndFeel(LookAndFeel laf)
          Adds a LookAndFeel to the list of auxiliary look and feels.
static void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
          Adds a PropertyChangeListener to the listener list.
private static void checkProperty(Properties props, String key)
           
static Object get(Object key)
          Returns an object from the defaults table.
static Object get(Object key, Locale l)
          Returns an object from the defaults table that is appropriate for the given locale.
static LookAndFeel[] getAuxiliaryLookAndFeels()
          Returns the list of auxiliary look and feels (can be null).
static boolean getBoolean(Object key)
          Returns a boolean from the defaults table which is associated with the key value.
static boolean getBoolean(Object key, Locale l)
          Returns a boolean from the defaults table which is associated with the key value and the given Locale.
static Border getBorder(Object key)
          Returns a border from the defaults table.
static Border getBorder(Object key, Locale l)
          Returns a border from the defaults table that is appropriate for the given locale.
static Color getColor(Object key)
          Returns a drawing color from the defaults table.
static Color getColor(Object key, Locale l)
          Returns a drawing color from the defaults table that is appropriate for the given locale.
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).
static UIDefaults getDefaults()
          Returns the default values for this look and feel.
static Dimension getDimension(Object key)
          Returns a dimension from the defaults table.
static Dimension getDimension(Object key, Locale l)
          Returns a dimension from the defaults table that is appropriate for the given locale.
static Font getFont(Object key)
          Returns a drawing font from the defaults table.
static Font getFont(Object key, Locale l)
          Returns a drawing font from the defaults table that is appropriate for the given locale.
static Icon getIcon(Object key)
          Returns an Icon from the defaults table.
static Icon getIcon(Object key, Locale l)
          Returns an Icon from the defaults table that is appropriate for the given locale.
static Insets getInsets(Object key)
          Returns an Insets object from the defaults table.
static Insets getInsets(Object key, Locale l)
          Returns an Insets object from the defaults table that is appropriate for the given locale.
static UIManager.LookAndFeelInfo[] getInstalledLookAndFeels()
          Returns an array of objects that provide some information about the LookAndFeel implementations that have been installed with this software development kit.
static int getInt(Object key)
          Returns an integer from the defaults table.
(package private) static int getInt(Object key, int defaultValue)
          Returns an integer from the defaults table.
static int getInt(Object key, Locale l)
          Returns an integer from the defaults table that is appropriate for the given locale.
private static UIManager.LAFState getLAFState()
          Return the LAFState object, lazily create one if necessary.
static LookAndFeel getLookAndFeel()
          Returns the current default look and feel or null.
static UIDefaults getLookAndFeelDefaults()
          Returns the default values for this look and feel.
private static LookAndFeel getMultiLookAndFeel()
          Finds the Multiplexing LookAndFeel.
static java.beans.PropertyChangeListener[] getPropertyChangeListeners()
          Returns an array of all the PropertyChangeListeners added to this UIManager with addPropertyChangeListener().
static String getString(Object key)
          Returns a string from the defaults table.
(package private) static String getString(Object key, Component c)
          Returns a string from the defaults table that is appropriate for the given locale.
static String getString(Object key, Locale l)
          Returns a string from the defaults table that is appropriate for the given locale.
static String getSystemLookAndFeelClassName()
          Returns the name of the LookAndFeel class that implements the native systems look and feel if there is one, otherwise the name of the default cross platform LookAndFeel class.
static ComponentUI getUI(JComponent target)
          Returns the L&F object that renders the target component.
private static void initialize()
           
private static void initializeAuxiliaryLAFs(Properties swingProps)
           
private static void initializeDefaultLAF(Properties swingProps)
          If the user has specified a default look and feel, use that.
private static void initializeInstalledLAFs(Properties swingProps)
          If a swing.properties file exist and it has a swing.installedlafs property then initialize the installedLAFs field.
private static void initializeSystemDefaults(Properties swingProps)
           
static void installLookAndFeel(String name, String className)
          Creates a new look and feel and adds it to the current array.
static void installLookAndFeel(UIManager.LookAndFeelInfo info)
          Adds the specified look and feel to the current array and then calls setInstalledLookAndFeels(javax.swing.UIManager.LookAndFeelInfo[]).
private static Properties loadSwingProperties()
           
private static String makeInstalledLAFKey(String laf, String attr)
          Return a swing.properties file key for the attribute of specified look and feel.
private static String makeSwingPropertiesFilename()
          The filename for swing.properties is a path like this (Unix version): /lib/swing.properties.
private static void maybeInitialize()
           
static Object put(Object key, Object value)
          Stores an object in the defaults table.
static boolean removeAuxiliaryLookAndFeel(LookAndFeel laf)
          Removes a LookAndFeel from the list of auxiliary look and feels.
static void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
          Removes a PropertyChangeListener from the listener list.
static void setInstalledLookAndFeels(UIManager.LookAndFeelInfo[] infos)
          Replaces the current array of installed LookAndFeelInfos.
static void setLookAndFeel(LookAndFeel newLookAndFeel)
          Sets the current default look and feel using a LookAndFeel object.
static void setLookAndFeel(String className)
          Sets the current default look and feel using a class name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lafStateACKey

private static final Object lafStateACKey
The AppContext key for our one LAFState instance.


classLock

private static final Object classLock

currentLAFStateThread

private static Thread currentLAFStateThread

currentLAFState

private static UIManager.LAFState currentLAFState

defaultLAFKey

private static final String defaultLAFKey
See Also:
Constant Field Values

auxiliaryLAFsKey

private static final String auxiliaryLAFsKey
See Also:
Constant Field Values

multiplexingLAFKey

private static final String multiplexingLAFKey
See Also:
Constant Field Values

installedLAFsKey

private static final String installedLAFsKey
See Also:
Constant Field Values

disableMnemonicKey

private static final String disableMnemonicKey
See Also:
Constant Field Values

installedLAFs

private static UIManager.LookAndFeelInfo[] installedLAFs
The default value of installedLAFS is used when no swing.properties file is available or if the file doesn't contain a "swing.installedlafs" property.

See Also:
initializeInstalledLAFs(java.util.Properties)
Constructor Detail

UIManager

public UIManager()
Method Detail

getLAFState

private static UIManager.LAFState getLAFState()
Return the LAFState object, lazily create one if necessary. All access to the LAFState fields is done via this method, for example:
     getLAFState().initialized = true;
 


makeInstalledLAFKey

private static String makeInstalledLAFKey(String laf,
                                          String attr)
Return a swing.properties file key for the attribute of specified look and feel. The attr is either "name" or "class", a typical key would be: "swing.installedlaf.windows.name"


makeSwingPropertiesFilename

private static String makeSwingPropertiesFilename()
The filename for swing.properties is a path like this (Unix version): /lib/swing.properties. This method returns a bogus filename if java.home isn't defined.


getInstalledLookAndFeels

public static UIManager.LookAndFeelInfo[] getInstalledLookAndFeels()
Returns an array of objects that provide some information about the LookAndFeel implementations that have been installed with this software development kit. The LookAndFeel info objects can used by an application to construct a menu of look and feel options for the user or to set the look and feel at start up time. Note that we do not return the LookAndFeel classes themselves here to avoid the cost of unnecessarily loading them.

Given a LookAndFeelInfo object one can set the current look and feel like this:

 UIManager.setLookAndFeel(info.getClassName());
 

Returns:
an array of LookAndFeelInfo objects
See Also:
setLookAndFeel(javax.swing.LookAndFeel)

setInstalledLookAndFeels

public static void setInstalledLookAndFeels(UIManager.LookAndFeelInfo[] infos)
                                     throws SecurityException
Replaces the current array of installed LookAndFeelInfos.

Parameters:
infos - new array of LookAndFeelInfo objects
Throws:
SecurityException
See Also:
getInstalledLookAndFeels()

installLookAndFeel

public static void installLookAndFeel(UIManager.LookAndFeelInfo info)
Adds the specified look and feel to the current array and then calls setInstalledLookAndFeels(javax.swing.UIManager.LookAndFeelInfo[]).

Parameters:
info - a LookAndFeelInfo object that names the look and feel and identifies that class that implements it

installLookAndFeel

public static void installLookAndFeel(String name,
                                      String className)
Creates a new look and feel and adds it to the current array. Then calls setInstalledLookAndFeels(javax.swing.UIManager.LookAndFeelInfo[]).

Parameters:
name - a String specifying the name of the look and feel
className - a String specifying the class name that implements the look and feel

getLookAndFeel

public static LookAndFeel getLookAndFeel()
Returns the current default look and feel or null.

Returns:
the current default look and feel, or null
See Also:
setLookAndFeel(javax.swing.LookAndFeel)

setLookAndFeel

public static void setLookAndFeel(LookAndFeel newLookAndFeel)
                           throws UnsupportedLookAndFeelException
Sets the current default look and feel using a LookAndFeel object.

This is a JavaBeans bound property.

Parameters:
newLookAndFeel - the LookAndFeel object
Throws:
UnsupportedLookAndFeelException - if lnf.isSupportedLookAndFeel() is false
See Also:
getLookAndFeel()

setLookAndFeel

public static void setLookAndFeel(String className)
                           throws ClassNotFoundException,
                                  InstantiationException,
                                  IllegalAccessException,
                                  UnsupportedLookAndFeelException
Sets the current default look and feel using a class name.

Parameters:
className - a string specifying the name of the class that implements the look and feel
Throws:
ClassNotFoundException - if the LookAndFeel class could not be found
InstantiationException - if a new instance of the class couldn't be created
IllegalAccessException - if the class or initializer isn't accessible
UnsupportedLookAndFeelException - if lnf.isSupportedLookAndFeel() is false

getSystemLookAndFeelClassName

public static String getSystemLookAndFeelClassName()
Returns the name of the LookAndFeel class that implements the native systems look and feel if there is one, otherwise the name of the default cross platform LookAndFeel class.

Returns:
the String of the LookAndFeel class
See Also:
setLookAndFeel(javax.swing.LookAndFeel), getCrossPlatformLookAndFeelClassName()

getCrossPlatformLookAndFeelClassName

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).

Returns:
a string with the JLF implementation-class
See Also:
setLookAndFeel(javax.swing.LookAndFeel), getSystemLookAndFeelClassName()

getDefaults

public static UIDefaults getDefaults()
Returns the default values for this look and feel.

Returns:
a UIDefaults object containing the default values

getFont

public static Font getFont(Object key)
Returns a drawing font from the defaults table.

Parameters:
key - an Object specifying the font
Returns:
the Font object

getFont

public static Font getFont(Object key,
                           Locale l)
Returns a drawing font from the defaults table that is appropriate for the given locale.

Parameters:
key - an Object specifying the font
l - the Locale for which the font is desired
Returns:
the Font object
Since:
1.4

getColor

public static Color getColor(Object key)
Returns a drawing color from the defaults table.

Parameters:
key - an Object specifying the color
Returns:
the Color object

getColor

public static Color getColor(Object key,
                             Locale l)
Returns a drawing color from the defaults table that is appropriate for the given locale.

Parameters:
key - an Object specifying the color
l - the Locale for which the color is desired
Returns:
the Color object
Since:
1.4

getIcon

public static Icon getIcon(Object key)
Returns an Icon from the defaults table.

Parameters:
key - an Object specifying the icon
Returns:
the Icon object

getIcon

public static Icon getIcon(Object key,
                           Locale l)
Returns an Icon from the defaults table that is appropriate for the given locale.

Parameters:
key - an Object specifying the icon
l - the Locale for which the icon is desired
Returns:
the Icon object
Since:
1.4

getBorder

public static Border getBorder(Object key)
Returns a border from the defaults table.

Parameters:
key - an Object specifying the border
Returns:
the Border object

getBorder

public static Border getBorder(Object key,
                               Locale l)
Returns a border from the defaults table that is appropriate for the given locale.

Parameters:
key - an Object specifying the border
l - the Locale for which the border is desired
Returns:
the Border object
Since:
1.4

getString

public static String getString(Object key)
Returns a string from the defaults table.

Parameters:
key - an Object specifying the string
Returns:
the String

getString

public static String getString(Object key,
                               Locale l)
Returns a string from the defaults table that is appropriate for the given locale.

Parameters:
key - an Object specifying the string
l - the Locale for which the string is desired
Returns:
the String

getString

static String getString(Object key,
                        Component c)
Returns a string from the defaults table that is appropriate for the given locale.

Parameters:
key - an Object specifying the string
c - Component used to determine Locale, null implies use the default Locale.
Returns:
the String

getInt

public static int getInt(Object key)
Returns an integer from the defaults table.

Parameters:
key - an Object specifying the int
Returns:
the int

getInt

public static int getInt(Object key,
                         Locale l)
Returns an integer from the defaults table that is appropriate for the given locale.

Parameters:
key - an Object specifying the int
l - the Locale for which the int is desired
Returns:
the int
Since:
1.4

getInt

static int getInt(Object key,
                  int defaultValue)
Returns an integer from the defaults table. If key does not map to a valid Integer, or can not be convered from a String to an integer, default is returned.

Parameters:
key - an Object specifying the int
defaultValue - Returned value if key is not available, or is not an Integer
Returns:
the int

getBoolean

public static boolean getBoolean(Object key)
Returns a boolean from the defaults table which is associated with the key value. If the key is not found or the key doesn't represent a boolean value then false will be returned.

Parameters:
key - an Object specifying the key for the desired boolean value
Returns:
the boolean value corresponding to the key
Since:
1.4

getBoolean

public static boolean getBoolean(Object key,
                                 Locale l)
Returns a boolean from the defaults table 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 false will be returned.

Parameters:
key - an Object specifying the key for the desired boolean value
l - the Locale for which the boolean is desired
Returns:
the boolean value corresponding to the key
Since:
1.4

getInsets

public static Insets getInsets(Object key)
Returns an Insets object from the defaults table.

Parameters:
key - an Object specifying the Insets object
Returns:
the Insets object

getInsets

public static Insets getInsets(Object key,
                               Locale l)
Returns an Insets object from the defaults table that is appropriate for the given locale.

Parameters:
key - an Object specifying the Insets object
l - the Locale for which the object is desired
Returns:
the Insets object
Since:
1.4

getDimension

public static Dimension getDimension(Object key)
Returns a dimension from the defaults table.

Parameters:
key - an Object specifying the dimension object
Returns:
the Dimension object

getDimension

public static Dimension getDimension(Object key,
                                     Locale l)
Returns a dimension from the defaults table that is appropriate for the given locale.

Parameters:
key - an Object specifying the dimension object
l - the Locale for which the object is desired
Returns:
the Dimension object
Since:
1.4

get

public static Object get(Object key)
Returns an object from the defaults table.

Parameters:
key - an Object specifying the desired object
Returns:
the Object

get

public static Object get(Object key,
                         Locale l)
Returns an object from the defaults table that is appropriate for the given locale.

Parameters:
key - an Object specifying the desired object
l - the Locale for which the object is desired
Returns:
the Object

put

public static Object put(Object key,
                         Object value)
Stores an object in the defaults table.

Parameters:
key - an Object specifying the retrieval key
value - the Object to store
Returns:
the Object returned by UIDefaults.put(java.lang.Object, java.lang.Object)

getUI

public static ComponentUI getUI(JComponent target)
Returns the L&F object that renders the target component.

Parameters:
target - the JComponent to render
Returns:
the ComponentUI object that renders the target component

getLookAndFeelDefaults

public static UIDefaults getLookAndFeelDefaults()
Returns the default values for this look and feel.

Returns:
an UIDefaults object containing the default values

getMultiLookAndFeel

private static LookAndFeel getMultiLookAndFeel()
Finds the Multiplexing LookAndFeel.


addAuxiliaryLookAndFeel

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.

Parameters:
laf - the LookAndFeel object
See Also:
removeAuxiliaryLookAndFeel(javax.swing.LookAndFeel), setLookAndFeel(javax.swing.LookAndFeel), getAuxiliaryLookAndFeels(), getInstalledLookAndFeels()

removeAuxiliaryLookAndFeel

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.

Returns:
true if the LookAndFeel was removed from the list
See Also:
removeAuxiliaryLookAndFeel(javax.swing.LookAndFeel), getAuxiliaryLookAndFeels(), setLookAndFeel(javax.swing.LookAndFeel), getInstalledLookAndFeels()

getAuxiliaryLookAndFeels

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.

Returns:
list of auxiliary LookAndFeels or null
See Also:
addAuxiliaryLookAndFeel(javax.swing.LookAndFeel), removeAuxiliaryLookAndFeel(javax.swing.LookAndFeel), setLookAndFeel(javax.swing.LookAndFeel), getInstalledLookAndFeels()

addPropertyChangeListener

public static void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
Adds a PropertyChangeListener to the listener list. The listener is registered for all properties.

Parameters:
listener - the PropertyChangeListener to be added
See Also:
PropertyChangeSupport

removePropertyChangeListener

public static void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
Removes a PropertyChangeListener from the listener list. This removes a PropertyChangeListener that was registered for all properties.

Parameters:
listener - the PropertyChangeListener to be removed
See Also:
PropertyChangeSupport

getPropertyChangeListeners

public static java.beans.PropertyChangeListener[] getPropertyChangeListeners()
Returns an array of all the PropertyChangeListeners added to this UIManager with addPropertyChangeListener().

Returns:
all of the PropertyChangeListeners added or an empty array if no listeners have been added
Since:
1.4

loadSwingProperties

private static Properties loadSwingProperties()

checkProperty

private static void checkProperty(Properties props,
                                  String key)

initializeInstalledLAFs

private static void initializeInstalledLAFs(Properties swingProps)
If a swing.properties file exist and it has a swing.installedlafs property then initialize the installedLAFs field.

See Also:
getInstalledLookAndFeels()

initializeDefaultLAF

private static void initializeDefaultLAF(Properties swingProps)
If the user has specified a default look and feel, use that. Otherwise use the look and feel that's native to this platform. If this code is called after the application has explicitly set it's look and feel, do nothing.

See Also:
maybeInitialize()

initializeAuxiliaryLAFs

private static void initializeAuxiliaryLAFs(Properties swingProps)

initializeSystemDefaults

private static void initializeSystemDefaults(Properties swingProps)

maybeInitialize

private static void maybeInitialize()

initialize

private static void initialize()