Class UIDefaults

All Implemented Interfaces:
Serializable, Cloneable, Map<Object,Object>

public class UIDefaults extends Hashtable<Object,Object>
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 JavaBeans has been added to the java.beans package. Please see XMLEncoder.

Since:
1.2
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    This class enables one to store an entry in the defaults table that's constructed each time it's looked up with one of the getXXX(key) methods.
    static class 
    LazyInputMap will create a InputMap in its createValue method.
    static interface 
    This class enables one to store an entry in the defaults table that isn't constructed until the first time it's looked up with one of the getXXX(key) methods.
    static class 
    This class provides an implementation of LazyValue which can be used to delay loading of the Class for the instance to be created.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an empty defaults table.
    UIDefaults(int initialCapacity, float loadFactor)
    Creates an empty defaults table with the specified initial capacity and load factor.
    UIDefaults(Object[] keyValueList)
    Creates a defaults table initialized with the specified key/value pairs.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds a PropertyChangeListener to the listener list.
    void
    Adds a resource bundle to the list of resource bundles that are searched for localized values.
    protected void
    firePropertyChange(String propertyName, Object oldValue, Object newValue)
    Support for reporting bound property changes.
    get(Object key)
    Returns the value for key.
    get(Object key, Locale l)
    Returns the value for key associated with the given locale.
    boolean
    If the value of key is boolean, return the boolean value, otherwise return false.
    boolean
    If the value of key for the given Locale is boolean, return the boolean value, otherwise return false.
    If the value of key is a Border return it, otherwise return null.
    If the value of key for the given Locale is a Border return it, otherwise return null.
    If the value of key is a Color return it, otherwise return null.
    If the value of key for the given Locale is a Color return it, otherwise return null.
    Returns the default locale.
    If the value of key is a Dimension return it, otherwise return null.
    If the value of key for the given Locale is a Dimension return it, otherwise return null.
    If the value of key is a Font return it, otherwise return null.
    If the value of key for the given Locale is a Font return it, otherwise return null.
    If the value of key is an Icon return it, otherwise return null.
    If the value of key for the given Locale is an Icon return it, otherwise return null.
    If the value of key is an Insets return it, otherwise return null.
    If the value of key for the given Locale is an Insets return it, otherwise return null.
    int
    If the value of key is an Integer return its integer value, otherwise return 0.
    int
    getInt(Object key, Locale l)
    If the value of key for the given Locale is an Integer return its integer value, otherwise return 0.
    Returns an array of all the PropertyChangeListeners added to this UIDefaults with addPropertyChangeListener().
    If the value of key is a String return it, otherwise return null.
    If the value of key for the given Locale is a String return it, otherwise return null.
    getUI(JComponent target)
    Creates an ComponentUI implementation for the specified component.
    Class<? extends ComponentUI>
    getUIClass(String uiClassID)
    Returns the L&F class that renders this component.
    Class<? extends ComponentUI>
    getUIClass(String uiClassID, ClassLoader uiClassLoader)
    The value of get(uidClassID) must be the String name of a class that implements the corresponding ComponentUI class.
    protected void
    If getUI() fails for any reason, it calls this method before returning null.
    put(Object key, Object value)
    Sets the value of key to value for all locales.
    void
    putDefaults(Object[] keyValueList)
    Puts all of the key/value pairs in the database and unconditionally generates one PropertyChangeEvent.
    void
    Removes a PropertyChangeListener from the listener list.
    void
    Removes a resource bundle from the list of resource bundles that are searched for localized defaults.
    void
    Sets the default locale.

    Methods declared in class Hashtable

    clear, clone, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, hashCode, isEmpty, keys, keySet, merge, putAll, rehash, remove, size, toString, values
    Modifier and Type
    Method
    Description
    void
    Clears this hashtable so that it contains no keys.
    Creates a shallow copy of this hashtable.
    compute(Object key, BiFunction<? super Object, ? super Object, ? extends Object> remappingFunction)
    Attempts to compute a mapping for the specified key and its current mapped value, or null if there is no current mapping (optional operation).
    computeIfAbsent(Object key, Function<? super Object, ? extends Object> mappingFunction)
    If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null (optional operation).
    computeIfPresent(Object key, BiFunction<? super Object, ? super Object, ? extends Object> remappingFunction)
    If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value (optional operation).
    boolean
    Tests if some key maps into the specified value in this hashtable.
    boolean
    Tests if the specified object is a key in this hashtable.
    boolean
    Returns true if this hashtable maps one or more keys to this value.
    Returns an enumeration of the values in this hashtable.
    Returns a Set view of the mappings contained in this map.
    boolean
    Compares the specified Object with this Map for equality, as per the definition in the Map interface.
    int
    Returns the hash code value for this Map as per the definition in the Map interface.
    boolean
    Tests if this hashtable maps no keys to values.
    Returns an enumeration of the keys in this hashtable.
    Returns a Set view of the keys contained in this map.
    merge(Object key, Object value, BiFunction<? super Object, ? super Object, ? extends Object> remappingFunction)
    If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value (optional operation).
    void
    putAll(Map<? extends Object, ? extends Object> t)
    Copies all of the mappings from the specified map to this hashtable.
    protected void
    Increases the capacity of and internally reorganizes this hashtable, in order to accommodate and access its entries more efficiently.
    Removes the key (and its corresponding value) from this hashtable.
    int
    Returns the number of keys in this hashtable.
    Returns a string representation of this Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters "" (comma and space).
    Returns a Collection view of the values contained in this map.

    Methods declared in class Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
    Modifier and Type
    Method
    Description
    protected void
    Deprecated, for removal: This API element is subject to removal in a future version.
    Finalization is deprecated and subject to removal in a future release.
    final Class<?>
    Returns the runtime class of this Object.
    final void
    Wakes up a single thread that is waiting on this object's monitor.
    final void
    Wakes up all threads that are waiting on this object's monitor.
    final void
    Causes the current thread to wait until it is awakened, typically by being notified or interrupted.
    final void
    wait(long timeoutMillis)
    Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
    final void
    wait(long timeoutMillis, int nanos)
    Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

    Methods declared in interface Map

    forEach, getOrDefault, putIfAbsent, remove, replace, replace, replaceAll
    Modifier and Type
    Method
    Description
    default void
    forEach(BiConsumer<? super Object, ? super Object> action)
    Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.
    default Object
    getOrDefault(Object key, Object defaultValue)
    Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
    default Object
    putIfAbsent(Object key, Object value)
    If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value (optional operation).
    default boolean
    remove(Object key, Object value)
    Removes the entry for the specified key only if it is currently mapped to the specified value (optional operation).
    default Object
    replace(Object key, Object value)
    Replaces the entry for the specified key only if it is currently mapped to some value (optional operation).
    default boolean
    replace(Object key, Object oldValue, Object newValue)
    Replaces the entry for the specified key only if currently mapped to the specified value (optional operation).
    default void
    replaceAll(BiFunction<? super Object, ? super Object, ? extends Object> function)
    Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception (optional operation).
  • Constructor Details

    • UIDefaults

      public UIDefaults()
      Creates an empty defaults table.
    • UIDefaults

      public UIDefaults(int initialCapacity, float loadFactor)
      Creates an empty defaults table with the specified initial capacity and load factor.
      Parameters:
      initialCapacity - the initial capacity of the defaults table
      loadFactor - the load factor of the defaults table
      Since:
      1.6
      See Also:
    • UIDefaults

      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", Integer.valueOf(5)
             }
             UIDefaults myDefaults = new UIDefaults(uiDefaults);
      
      Parameters:
      keyValueList - an array of objects containing the key/value pairs
  • Method Details

    • get

      public Object get(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.
      Specified by:
      get in interface Map<Object,Object>
      Overrides:
      get in class Hashtable<Object,Object>
      Parameters:
      key - the desired key
      Returns:
      the value for key
      Since:
      1.4
      See Also:
    • get

      public Object get(Object key, 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.
      Parameters:
      key - the desired key
      l - the desired locale
      Returns:
      the value for key
      Since:
      1.4
      See Also:
    • put

      public Object put(Object key, 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.
      Specified by:
      put in interface Map<Object,Object>
      Overrides:
      put in class Hashtable<Object,Object>
      Parameters:
      key - the unique Object who's value will be used to retrieve the data value associated with it
      value - the new Object to store as data under that key
      Returns:
      the previous Object value, or null
      See Also:
    • putDefaults

      public void putDefaults(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.
      Parameters:
      keyValueList - an array of key/value pairs
      See Also:
    • getFont

      public Font getFont(Object key)
      If the value of key is a Font return it, otherwise return null.
      Parameters:
      key - the desired key
      Returns:
      if the value for key is a Font, return the Font object; otherwise return null
    • getFont

      public Font getFont(Object key, Locale l)
      If the value of key for the given Locale is a Font return it, otherwise return null.
      Parameters:
      key - the desired key
      l - the desired locale
      Returns:
      if the value for key and Locale is a Font, return the Font object; otherwise return null
      Since:
      1.4
    • getColor

      public Color getColor(Object key)
      If the value of key is a Color return it, otherwise return null.
      Parameters:
      key - the desired key
      Returns:
      if the value for key is a Color, return the Color object; otherwise return null
    • getColor

      public Color getColor(Object key, Locale l)
      If the value of key for the given Locale is a Color return it, otherwise return null.
      Parameters:
      key - the desired key
      l - the desired locale
      Returns:
      if the value for key and Locale is a Color, return the Color object; otherwise return null
      Since:
      1.4
    • getIcon

      public Icon getIcon(Object key)
      If the value of key is an Icon return it, otherwise return null.
      Parameters:
      key - the desired key
      Returns:
      if the value for key is an Icon, return the Icon object; otherwise return null
    • getIcon

      public Icon getIcon(Object key, Locale l)
      If the value of key for the given Locale is an Icon return it, otherwise return null.
      Parameters:
      key - the desired key
      l - the desired locale
      Returns:
      if the value for key and Locale is an Icon, return the Icon object; otherwise return null
      Since:
      1.4
    • getBorder

      public Border getBorder(Object key)
      If the value of key is a Border return it, otherwise return null.
      Parameters:
      key - the desired key
      Returns:
      if the value for key is a Border, return the Border object; otherwise return null
    • getBorder

      public Border getBorder(Object key, Locale l)
      If the value of key for the given Locale is a Border return it, otherwise return null.
      Parameters:
      key - the desired key
      l - the desired locale
      Returns:
      if the value for key and Locale is a Border, return the Border object; otherwise return null
      Since:
      1.4
    • getString

      public String getString(Object key)
      If the value of key is a String return it, otherwise return null.
      Parameters:
      key - the desired key
      Returns:
      if the value for key is a String, return the String object; otherwise return null
    • getString

      public String getString(Object key, Locale l)
      If the value of key for the given Locale is a String return it, otherwise return null.
      Parameters:
      key - the desired key
      l - the desired Locale
      Returns:
      if the value for key for the given Locale is a String, return the String object; otherwise return null
      Since:
      1.4
    • getInt

      public int getInt(Object key)
      If the value of key is an Integer return its integer value, otherwise return 0.
      Parameters:
      key - the desired key
      Returns:
      if the value for key is an Integer, return its value, otherwise return 0
    • getInt

      public int getInt(Object key, Locale l)
      If the value of key for the given Locale is an Integer return its integer value, otherwise return 0.
      Parameters:
      key - the desired key
      l - the desired locale
      Returns:
      if the value for key and Locale is an Integer, return its value, otherwise return 0
      Since:
      1.4
    • getBoolean

      public boolean getBoolean(Object key)
      If the value of key is boolean, return the boolean value, otherwise return false.
      Parameters:
      key - an Object specifying the key for the desired boolean value
      Returns:
      if the value of key is boolean, return the boolean value, otherwise return false.
      Since:
      1.4
    • getBoolean

      public boolean getBoolean(Object key, Locale l)
      If the value of key for the given Locale is boolean, return the boolean value, otherwise return false.
      Parameters:
      key - an Object specifying the key for the desired boolean value
      l - the desired locale
      Returns:
      if the value for key and Locale is boolean, return the boolean value, otherwise return false.
      Since:
      1.4
    • getInsets

      public Insets getInsets(Object key)
      If the value of key is an Insets return it, otherwise return null.
      Parameters:
      key - the desired key
      Returns:
      if the value for key is an Insets, return the Insets object; otherwise return null
    • getInsets

      public Insets getInsets(Object key, Locale l)
      If the value of key for the given Locale is an Insets return it, otherwise return null.
      Parameters:
      key - the desired key
      l - the desired locale
      Returns:
      if the value for key and Locale is an Insets, return the Insets object; otherwise return null
      Since:
      1.4
    • getDimension

      public Dimension getDimension(Object key)
      If the value of key is a Dimension return it, otherwise return null.
      Parameters:
      key - the desired key
      Returns:
      if the value for key is a Dimension, return the Dimension object; otherwise return null
    • getDimension

      public Dimension getDimension(Object key, Locale l)
      If the value of key for the given Locale is a Dimension return it, otherwise return null.
      Parameters:
      key - the desired key
      l - the desired locale
      Returns:
      if the value for key and Locale is a Dimension, return the Dimension object; otherwise return null
      Since:
      1.4
    • getUIClass

      public Class<? extends ComponentUI> getUIClass(String uiClassID, 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.

      Parameters:
      uiClassID - a string containing the class ID
      uiClassLoader - the object which will load the class
      Returns:
      the value of Class.forName(get(uidClassID))
      See Also:
    • getUIClass

      public Class<? extends ComponentUI> getUIClass(String uiClassID)
      Returns the L&F class that renders this component.
      Parameters:
      uiClassID - a string containing the class ID
      Returns:
      the Class object returned by getUIClass(uiClassID, null)
    • getUIError

      protected void getUIError(String msg)
      If getUI() fails for any reason, it calls this method before returning null. Subclasses may choose to do more or less here.
      Parameters:
      msg - message string to print
      See Also:
    • getUI

      public ComponentUI getUI(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.
      Parameters:
      target - the JComponent which needs a UI
      Returns:
      the ComponentUI object
    • addPropertyChangeListener

      public void addPropertyChangeListener(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.

      Parameters:
      listener - the PropertyChangeListener to be added
      See Also:
    • removePropertyChangeListener

      public void removePropertyChangeListener(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:
    • getPropertyChangeListeners

      public PropertyChangeListener[] getPropertyChangeListeners()
      Returns an array of all the PropertyChangeListeners added to this UIDefaults with addPropertyChangeListener().
      Returns:
      all of the PropertyChangeListeners added or an empty array if no listeners have been added
      Since:
      1.4
    • firePropertyChange

      protected void firePropertyChange(String propertyName, Object oldValue, 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.
      Parameters:
      propertyName - the programmatic name of the property that was changed
      oldValue - the old value of the property
      newValue - the new value of the property
      See Also:
    • addResourceBundle

      public void addResourceBundle(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, using the system class loader. In other words, the most recently added bundle is searched first.
      Parameters:
      bundleName - the base name of the resource bundle to be added
      Since:
      1.4
      See Also:
    • removeResourceBundle

      public void removeResourceBundle(String bundleName)
      Removes a resource bundle from the list of resource bundles that are searched for localized defaults.
      Parameters:
      bundleName - the base name of the resource bundle to be removed
      Since:
      1.4
      See Also:
    • setDefaultLocale

      public void setDefaultLocale(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.
      Parameters:
      l - the new default locale
      Since:
      1.4
      See Also:
    • getDefaultLocale

      public Locale getDefaultLocale()
      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.
      Returns:
      the default locale
      Since:
      1.4
      See Also: