FileDocCategorySizeDatePackage
UIDefaults.javaAPI DocJava SE 6 API42166Tue Jun 10 00:26:42 BST 2008javax.swing

UIDefaults

public class UIDefaults extends Hashtable
A table of defaults for Swing components. Applications can set/get default values via the UIManager.

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 {@link java.beans.XMLEncoder}.

see
UIManager
version
1.64 03/15/07
author
Hans Muller

Fields Summary
private static final Object
PENDING
private SwingPropertyChangeSupport
changeSupport
private Vector
resourceBundles
private Locale
defaultLocale
private Map
resourceCache
Maps from a Locale to a cached Map of the ResourceBundle. This is done so as to avoid an exception being thrown when a value is asked for. Access to this should be done while holding a lock on the UIDefaults, eg synchronized(this).
Constructors Summary
public UIDefaults()
Creates an empty defaults table.


              
      
        this(700, .75f);
    
public UIDefaults(int initialCapacity, float loadFactor)
Creates an empty defaults table with the specified initial capacity and load factor.

param
initialCapacity the initial capacity of the defaults table
param
loadFactor the load factor of the defaults table
see
java.util.Hashtable
since
1.6

        super(initialCapacity, loadFactor);
        resourceCache = new HashMap();
    
public UIDefaults(Object[] keyValueList)
Creates a defaults table initialized with the specified key/value pairs. For example:
Object[] uiDefaults = {
"Font", new Font("Dialog", Font.BOLD, 12),
"Color", Color.red,
"five", new Integer(5)
}
UIDefaults myDefaults = new UIDefaults(uiDefaults);

param
keyValueList an array of objects containing the key/value pairs

        super(keyValueList.length / 2);
        for(int i = 0; i < keyValueList.length; i += 2) {
            super.put(keyValueList[i], keyValueList[i + 1]);
        }
    
Methods Summary
public synchronized voidaddPropertyChangeListener(java.beans.PropertyChangeListener listener)
Adds a PropertyChangeListener to the listener list. The listener is registered for all properties.

A PropertyChangeEvent will get fired whenever a default is changed.

param
listener the PropertyChangeListener to be added
see
java.beans.PropertyChangeSupport

        if (changeSupport == null) {
            changeSupport = new SwingPropertyChangeSupport(this);
        }
        changeSupport.addPropertyChangeListener(listener);
    
public synchronized voidaddResourceBundle(java.lang.String bundleName)
Adds a resource bundle to the list of resource bundles that are searched for localized values. Resource bundles are searched in the reverse order they were added. In other words, the most recently added bundle is searched first.

param
bundleName the base name of the resource bundle to be added
see
java.util.ResourceBundle
see
#removeResourceBundle
since
1.4

        if( bundleName == null ) {
            return;
        }
        if( resourceBundles == null ) {
            resourceBundles = new Vector(5);
        }
        if (!resourceBundles.contains(bundleName)) {
            resourceBundles.add( bundleName );
            resourceCache.clear();
        }
    
protected voidfirePropertyChange(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue)
Support for reporting bound property changes. If oldValue and newValue are not equal and the PropertyChangeEventx listener list isn't empty, then fire a PropertyChange event to each listener.

param
propertyName the programmatic name of the property that was changed
param
oldValue the old value of the property
param
newValue the new value of the property
see
java.beans.PropertyChangeSupport

        if (changeSupport != null) {
            changeSupport.firePropertyChange(propertyName, oldValue, newValue);
        }
    
public java.lang.Objectget(java.lang.Object key)
Returns the value for key. If the value is a UIDefaults.LazyValue then the real value is computed with LazyValue.createValue(), the table entry is replaced, and the real value is returned. If the value is an UIDefaults.ActiveValue the table entry is not replaced - the value is computed with ActiveValue.createValue() for each get() call. If the key is not found in the table then it is searched for in the list of resource bundles maintained by this object. The resource bundles are searched most recently added first using the locale returned by getDefaultLocale. LazyValues and ActiveValues are not supported in the resource bundles.

param
key the desired key
return
the value for key
see
LazyValue
see
ActiveValue
see
java.util.Hashtable#get
see
#getDefaultLocale
see
#addResourceBundle
since
1.4

        Object value = getFromHashtable( key );
        return (value != null) ? value : getFromResourceBundle(key, null);
    
public java.lang.Objectget(java.lang.Object key, java.util.Locale l)
Returns the value for key associated with the given locale. If the value is a UIDefaults.LazyValue then the real value is computed with LazyValue.createValue(), the table entry is replaced, and the real value is returned. If the value is an UIDefaults.ActiveValue the table entry is not replaced - the value is computed with ActiveValue.createValue() for each get() call. If the key is not found in the table then it is searched for in the list of resource bundles maintained by this object. The resource bundles are searched most recently added first using the given locale. LazyValues and ActiveValues are not supported in the resource bundles.

param
key the desired key
param
l the desired locale
return
the value for key
see
LazyValue
see
ActiveValue
see
java.util.Hashtable#get
see
#addResourceBundle
since
1.4

        Object value = getFromHashtable( key );
        return (value != null) ? value : getFromResourceBundle(key, l);
    
public booleangetBoolean(java.lang.Object key)
If the value of key is boolean, return the boolean value, otherwise return false.

param
key an Object specifying the key for the desired boolean value
return
if the value of key is boolean, return the boolean value, otherwise return false.
since
1.4

        Object value = get(key);
        return (value instanceof Boolean) ? ((Boolean)value).booleanValue() : false;
    
public booleangetBoolean(java.lang.Object key, java.util.Locale l)
If the value of key for the given Locale is boolean, return the boolean value, otherwise return false.

param
key an Object specifying the key for the desired boolean value
param
l the desired locale
return
if the value for key and Locale is boolean, return the boolean value, otherwise return false.
since
1.4

        Object value = get(key,l);
        return (value instanceof Boolean) ? ((Boolean)value).booleanValue() : false;
    
public javax.swing.border.BordergetBorder(java.lang.Object key)
If the value of key is a Border return it, otherwise return null.

param
key the desired key
return
if the value for key is a Border, return the Border object; otherwise return null

        Object value = get(key);
        return (value instanceof Border) ? (Border)value : null;
    
public javax.swing.border.BordergetBorder(java.lang.Object key, java.util.Locale l)
If the value of key for the given Locale is a Border return it, otherwise return null.

param
key the desired key
param
l the desired locale
return
if the value for key and Locale is a Border, return the Border object; otherwise return null
since
1.4

        Object value = get(key,l);
        return (value instanceof Border) ? (Border)value : null;
    
public java.awt.ColorgetColor(java.lang.Object key)
If the value of key is a Color return it, otherwise return null.

param
key the desired key
return
if the value for key is a Color, return the Color object; otherwise return null

        Object value = get(key);
        return (value instanceof Color) ? (Color)value : null;
    
public java.awt.ColorgetColor(java.lang.Object key, java.util.Locale l)
If the value of key for the given Locale is a Color return it, otherwise return null.

param
key the desired key
param
l the desired locale
return
if the value for key and Locale is a Color, return the Color object; otherwise return null
since
1.4

        Object value = get(key,l);
        return (value instanceof Color) ? (Color)value : null;
    
public java.util.LocalegetDefaultLocale()
Returns the default locale. The default locale is used in retrieving localized values via get methods that do not take a locale argument. As of release 1.4, Swing UI objects should retrieve localized values using the locale of their component rather than the default locale. The default locale exists to provide compatibility with pre 1.4 behaviour.

return
the default locale
see
#setDefaultLocale
see
#get(Object)
see
#get(Object,Locale)
since
1.4

        return defaultLocale;
    
public java.awt.DimensiongetDimension(java.lang.Object key)
If the value of key is a Dimension return it, otherwise return null.

param
key the desired key
return
if the value for key is a Dimension, return the Dimension object; otherwise return null

        Object value = get(key);
        return (value instanceof Dimension) ? (Dimension)value : null;
    
public java.awt.DimensiongetDimension(java.lang.Object key, java.util.Locale l)
If the value of key for the given Locale is a Dimension return it, otherwise return null.

param
key the desired key
param
l the desired locale
return
if the value for key and Locale is a Dimension, return the Dimension object; otherwise return null
since
1.4

        Object value = get(key,l);
        return (value instanceof Dimension) ? (Dimension)value : null;
    
public java.awt.FontgetFont(java.lang.Object key)
If the value of key is a Font return it, otherwise return null.

param
key the desired key
return
if the value for key is a Font, return the Font object; otherwise return null

        Object value = get(key);
        return (value instanceof Font) ? (Font)value : null;
    
public java.awt.FontgetFont(java.lang.Object key, java.util.Locale l)
If the value of key for the given Locale is a Font return it, otherwise return null.

param
key the desired key
param
l the desired locale
return
if the value for key and Locale is a Font, return the Font object; otherwise return null
since
1.4

        Object value = get(key,l);
        return (value instanceof Font) ? (Font)value : null;
    
private java.lang.ObjectgetFromHashtable(java.lang.Object key)
Looks up up the given key in our Hashtable and resolves LazyValues or ActiveValues.

        /* Quickly handle the common case, without grabbing
         * a lock.
         */
        Object value = super.get(key);
        if ((value != PENDING) &&
            !(value instanceof ActiveValue) &&
            !(value instanceof LazyValue)) {
            return value;
        }

        /* If the LazyValue for key is being constructed by another
         * thread then wait and then return the new value, otherwise drop
         * the lock and construct the ActiveValue or the LazyValue.
         * We use the special value PENDING to mark LazyValues that
         * are being constructed.
         */
        synchronized(this) {
            value = super.get(key);
            if (value == PENDING) {
                do {
                    try {
                        this.wait();
                    }
                    catch (InterruptedException e) {
                    }
                    value = super.get(key);
                }
                while(value == PENDING);
                return value;
            }
            else if (value instanceof LazyValue) {
                super.put(key, PENDING);
            }
            else if (!(value instanceof ActiveValue)) {
                return value;
            }
        }

        /* At this point we know that the value of key was
         * a LazyValue or an ActiveValue.
         */
        if (value instanceof LazyValue) {
            try {
                /* If an exception is thrown we'll just put the LazyValue
                 * back in the table.
                 */
                value = ((LazyValue)value).createValue(this);
            }
            finally {
                synchronized(this) {
                    if (value == null) {
                        super.remove(key);
                    }
                    else {
                        super.put(key, value);
                    }
                    this.notifyAll();
                }
            }
        }
        else {
            value = ((ActiveValue)value).createValue(this);
        }

        return value;
    
private java.lang.ObjectgetFromResourceBundle(java.lang.Object key, java.util.Locale l)
Looks up given key in our resource bundles.


        if( resourceBundles == null ||
            resourceBundles.isEmpty() ||
            !(key instanceof String) ) {
            return null;
        }

        // A null locale means use the default locale.
        if( l == null ) {
            if( defaultLocale == null )
                return null;
            else
                l = (Locale)defaultLocale;
        }

        synchronized(this) {
            return getResourceCache(l).get((String)key);
        }
    
public javax.swing.IcongetIcon(java.lang.Object key)
If the value of key is an Icon return it, otherwise return null.

param
key the desired key
return
if the value for key is an Icon, return the Icon object; otherwise return null

        Object value = get(key);
        return (value instanceof Icon) ? (Icon)value : null;
    
public javax.swing.IcongetIcon(java.lang.Object key, java.util.Locale l)
If the value of key for the given Locale is an Icon return it, otherwise return null.

param
key the desired key
param
l the desired locale
return
if the value for key and Locale is an Icon, return the Icon object; otherwise return null
since
1.4

        Object value = get(key,l);
        return (value instanceof Icon) ? (Icon)value : null;
    
public java.awt.InsetsgetInsets(java.lang.Object key)
If the value of key is an Insets return it, otherwise return null.

param
key the desired key
return
if the value for key is an Insets, return the Insets object; otherwise return null

        Object value = get(key);
        return (value instanceof Insets) ? (Insets)value : null;
    
public java.awt.InsetsgetInsets(java.lang.Object key, java.util.Locale l)
If the value of key for the given Locale is an Insets return it, otherwise return null.

param
key the desired key
param
l the desired locale
return
if the value for key and Locale is an Insets, return the Insets object; otherwise return null
since
1.4

        Object value = get(key,l);
        return (value instanceof Insets) ? (Insets)value : null;
    
public intgetInt(java.lang.Object key)
If the value of key is an Integer return its integer value, otherwise return 0.

param
key the desired key
return
if the value for key is an Integer, return its value, otherwise return 0

        Object value = get(key);
        return (value instanceof Integer) ? ((Integer)value).intValue() : 0;
    
public intgetInt(java.lang.Object key, java.util.Locale l)
If the value of key for the given Locale is an Integer return its integer value, otherwise return 0.

param
key the desired key
param
l the desired locale
return
if the value for key and Locale is an Integer, return its value, otherwise return 0
since
1.4

        Object value = get(key,l);
        return (value instanceof Integer) ? ((Integer)value).intValue() : 0;
    
public synchronized java.beans.PropertyChangeListener[]getPropertyChangeListeners()
Returns an array of all the PropertyChangeListeners added to this UIDefaults with addPropertyChangeListener().

return
all of the PropertyChangeListeners added or an empty array if no listeners have been added
since
1.4

        if (changeSupport == null) {
            return new PropertyChangeListener[0];
        }
        return changeSupport.getPropertyChangeListeners();
    
private java.util.MapgetResourceCache(java.util.Locale l)
Returns a Map of the known resources for the given locale.

        Map values = (Map)resourceCache.get(l);

        if (values == null) {
            values = new HashMap();
            for (int i=resourceBundles.size()-1; i >= 0; i--) {
                String bundleName = (String)resourceBundles.get(i);
                try {
		    Control c = CoreResourceBundleControl.getRBControlInstance(bundleName);
                    ResourceBundle b;
		    if (c != null) {
                        b = ResourceBundle.getBundle(bundleName, l, c);
		    } else {
                        b = ResourceBundle.getBundle(bundleName, l);
		    }
                    Enumeration keys = b.getKeys();

                    while (keys.hasMoreElements()) {
                        String key = (String)keys.nextElement();

                        if (values.get(key) == null) {
                            Object value = b.getObject(key);

                            values.put(key, value);
                        }
                    }
                } catch( MissingResourceException mre ) {
                    // Keep looking
                }
            }
            resourceCache.put(l, values);
        }
        return values;
    
public java.lang.StringgetString(java.lang.Object key)
If the value of key is a String return it, otherwise return null.

param
key the desired key
return
if the value for key is a String, return the String object; otherwise return null

        Object value = get(key);
        return (value instanceof String) ? (String)value : null;
    
public java.lang.StringgetString(java.lang.Object key, java.util.Locale l)
If the value of key for the given Locale is a String return it, otherwise return null.

param
key the desired key
param
l the desired Locale
return
if the value for key for the given Locale is a String, return the String object; otherwise return null
since
1.4

        Object value = get(key,l);
        return (value instanceof String) ? (String)value : null;
    
public javax.swing.plaf.ComponentUIgetUI(javax.swing.JComponent target)
Creates an ComponentUI implementation for the specified component. In other words create the look and feel specific delegate object for target. This is done in two steps:
  • Look up the name of the ComponentUI implementation class under the value returned by target.getUIClassID().
  • Use the implementation classes static createUI() method to construct a look and feel delegate.

param
target the JComponent which needs a UI
return
the ComponentUI object


        Object cl = get("ClassLoader");
	ClassLoader uiClassLoader = 
	    (cl != null) ? (ClassLoader)cl : target.getClass().getClassLoader();
        Class uiClass = getUIClass(target.getUIClassID(), uiClassLoader);
        Object uiObject = null;

        if (uiClass == null) {
            getUIError("no ComponentUI class for: " + target);
        }
        else {
            try {
		Method m = (Method)get(uiClass);
		if (m == null) {
		    Class acClass = javax.swing.JComponent.class;
		    m = uiClass.getMethod("createUI", new Class[]{acClass});
		    put(uiClass, m);
		}
		uiObject = MethodUtil.invoke(m, null, new Object[]{target});
            }
            catch (NoSuchMethodException e) {
                getUIError("static createUI() method not found in " + uiClass);
            }
            catch (Exception e) {
                getUIError("createUI() failed for " + target + " " + e);
            }
        }

        return (ComponentUI)uiObject;
    
public java.lang.ClassgetUIClass(java.lang.String uiClassID, java.lang.ClassLoader uiClassLoader)
The value of get(uidClassID) must be the String name of a class that implements the corresponding ComponentUI class. If the class hasn't been loaded before, this method looks up the class with uiClassLoader.loadClass() if a non null class loader is provided, classForName() otherwise.

If a mapping for uiClassID exists or if the specified class can't be found, return null.

This method is used by getUI, it's usually not necessary to call it directly.

param
uiClassID a string containing the class ID
param
uiClassLoader the object which will load the class
return
the value of Class.forName(get(uidClassID))
see
#getUI

        try {
            String className = (String)get(uiClassID);
            if (className != null) {
                Class cls = (Class)get(className);
                if (cls == null) {
                    if (uiClassLoader == null) {
                        cls = SwingUtilities.loadSystemClass(className);
                    }
                    else {
                        cls = uiClassLoader.loadClass(className);
                    }
                    if (cls != null) {
                        // Save lookup for future use, as forName is slow.
                        put(className, cls);
                    }
                }
                return cls;
            }
        } 
	catch (ClassNotFoundException e) {
            return null;
        } 
	catch (ClassCastException e) {
            return null;
        }
        return null;
    
public java.lang.ClassgetUIClass(java.lang.String uiClassID)
Returns the L&F class that renders this component.

param
uiClassID a string containing the class ID
return
the Class object returned by getUIClass(uiClassID, null)

	return getUIClass(uiClassID, null);
    
protected voidgetUIError(java.lang.String msg)
If getUI() fails for any reason, it calls this method before returning null. Subclasses may choose to do more or less here.

param
msg message string to print
see
#getUI

        System.err.println("UIDefaults.getUI() failed: " + msg);
        try {
            throw new Error();
        }
        catch (Throwable e) {
            e.printStackTrace();
        }
    
public java.lang.Objectput(java.lang.Object key, java.lang.Object value)
Sets the value of key to value for all locales. If key is a string and the new value isn't equal to the old one, fire a PropertyChangeEvent. If value is null, the key is removed from the table.

param
key the unique Object who's value will be used to retrieve the data value associated with it
param
value the new Object to store as data under that key
return
the previous Object value, or null
see
#putDefaults
see
java.util.Hashtable#put

        Object oldValue = (value == null) ? super.remove(key) : super.put(key, value);
        if (key instanceof String) {
            firePropertyChange((String)key, oldValue, value);
        }
        return oldValue;
    
public voidputDefaults(java.lang.Object[] keyValueList)
Puts all of the key/value pairs in the database and unconditionally generates one PropertyChangeEvent. The events oldValue and newValue will be null and its propertyName will be "UIDefaults". The key/value pairs are added for all locales.

param
keyValueList an array of key/value pairs
see
#put
see
java.util.Hashtable#put

        for(int i = 0, max = keyValueList.length; i < max; i += 2) {
            Object value = keyValueList[i + 1];
            if (value == null) {
                super.remove(keyValueList[i]);
            }
            else {
                super.put(keyValueList[i], value);
            }
        }
        firePropertyChange("UIDefaults", null, null);
    
public synchronized voidremovePropertyChangeListener(java.beans.PropertyChangeListener listener)
Removes a PropertyChangeListener from the listener list. This removes a PropertyChangeListener that was registered for all properties.

param
listener the PropertyChangeListener to be removed
see
java.beans.PropertyChangeSupport

        if (changeSupport != null) {
            changeSupport.removePropertyChangeListener(listener);
        }
    
public synchronized voidremoveResourceBundle(java.lang.String bundleName)
Removes a resource bundle from the list of resource bundles that are searched for localized defaults.

param
bundleName the base name of the resource bundle to be removed
see
java.util.ResourceBundle
see
#addResourceBundle
since
1.4

        if( resourceBundles != null ) {
            resourceBundles.remove( bundleName );
        }
        resourceCache.clear();
    
public voidsetDefaultLocale(java.util.Locale l)
Sets the default locale. The default locale is used in retrieving localized values via get methods that do not take a locale argument. As of release 1.4, Swing UI objects should retrieve localized values using the locale of their component rather than the default locale. The default locale exists to provide compatibility with pre 1.4 behaviour.

param
l the new default locale
see
#getDefaultLocale
see
#get(Object)
see
#get(Object,Locale)
since
1.4

        defaultLocale = l;