LookAndFeel, as the name implies, encapsulates a look and
feel. Beyond installing a look and feel most developers never need to
interact directly with LookAndFeel. In general only developers
creating a custom look and feel need to concern themselves with this class.
Swing is built upon the foundation that each JComponent
subclass has an implementation of a specific ComponentUI
subclass. The ComponentUI is often referred to as "the ui",
"component ui", or "look and feel delegate". The ComponentUI
subclass is responsible for providing the look and feel specific
functionality of the component. For example, JTree requires
an implementation of the ComponentUI subclass TreeUI. The implementation of the specific ComponentUI subclass is provided by the LookAndFeel. Each
JComponent subclass identifies the ComponentUI
subclass it requires by way of the JComponent method getUIClassID.
Each LookAndFeel implementation must provide
an implementation of the appropriate ComponentUI subclass by
specifying a value for each of Swing's ui class ids in the UIDefaults object returned from getDefaults. For example,
BasicLookAndFeel uses BasicTreeUI as the concrete
implementation for TreeUI. This is accomplished by BasicLookAndFeel providing the key-value pair "TreeUI"-"javax.swing.plaf.basic.BasicTreeUI", in the
UIDefaults returned from getDefaults. Refer to
UIDefaults#getUI(JComponent) for details on how the implementation
of the ComponentUI subclass is obtained.
When a LookAndFeel is installed the UIManager does
not check that an entry exists for all ui class ids. As such,
random exceptions will occur if the current look and feel has not
provided a value for a particular ui class id and an instance of
the JComponent subclass is created.
UIManager each LookAndFeel has the opportunity
to provide a set of defaults that are layered in with developer and
system defaults. Some of Swing's components require the look and feel
to provide a specific set of defaults. These are documented in the
classes that require the specific default.
ComponentUIs typically need to set various properties
on the JComponent the ComponentUI is providing the
look and feel for. This is typically done when the ComponentUI is installed on the JComponent. Setting a
property should only be done if the developer has not set the
property. For non-primitive values it is recommended that the
ComponentUI only change the property on the JComponent if the current value is null or implements
UIResource. If the current value is null or
implements UIResource it indicates the property has not
been set by the developer, and the ui is free to change it. For
example, BasicButtonUI.installDefaults only changes the
font on the JButton if the return value from button.getFont() is null or implements UIResource. On the other hand if button.getFont() returned
a non-null value that did not implement UIResource
then BasicButtonUI.installDefaults would not change the
JButton's font.
For primitive values, such as opaque, the method installProperty should be invoked. installProperty only changes
the corresponding property if the value has not been changed by the
developer.
ComponentUI implementations should use the various install methods
provided by this class as they handle the necessary checking and install
the property using the recommended guidelines.
LookAndFeel need to
access the defaults if the value of the property being changed is
null or a UIResource. For example, installing the
font does the following:
JComponent c;
Font font = c.getFont();
if (font == null || (font instanceof UIResource)) {
c.setFont(UIManager.getFont("fontKey"));
}
If the font is null or a UIResource, the
defaults table is queried with the key fontKey. All of
UIDefault's get methods throw a NullPointerException if passed in null. As such, unless
otherwise noted each of the various install methods of LookAndFeel throw a NullPointerException if the current
value is null or a UIResource and the supplied
defaults key is null. In addition, unless otherwise specified
all of the install methods throw a NullPointerException if
a null component is passed in.
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public UIDefaults | |
| public abstract String | Returns: short description for the look and feelReturn a one line description of this look and feel implementation, e.g. "The CDE/Motif Look and Feel". |
| public static Object | Returns: the current value of the desktop propertythe name of the system desktop property being queried systemPropertyName, Object the object to be returned as the value if the system value is null fallbackValue)Returns the value of the specified system desktop property by
invoking |
| public Icon | Returns: disabledIcon, or null if a suitable
Icon can not be generatedJComponent that will display the Icon,
may be nullIcon to generate the disabled icon fromReturns an |
| public Icon | Returns: disabled and selected icon, ornull if a suitable
Icon can not be generated.JComponent that will display the Icon,
may be nullIcon to generate disabled and selected icon fromReturns an |
| public abstract String | |
| public LayoutStyle | Returns: theLayoutStyle for this look and feelReturns the |
| public abstract String | Returns: short identifier for the look and feelReturn a short string that identifies this look and feel, e.g. "CDE/Motif". |
| public boolean | Returns: true if the RootPaneUI instances created by
this look and feel support client side decorationsReturns |
| public void | |
| public static void | installBorder(JComponent
component to set the border on c, String key specifying the border defaultBorderName)Convenience method for setting a component's border property with a value from the defaults. |
| public static void | installColors(JComponent
component to set the colors on c, String key for the background defaultBgName, String key for the foreground defaultFgName)Convenience method for setting a component's foreground and background color properties with values from the defaults. |
| public static void | installColorsAndFont(JComponent
component set to the colors and font on c, String key for the background defaultBgName, String key for the foreground defaultFgName, String key for the font defaultFontName)Convenience method for setting a component's foreground, background and font properties with values from the defaults. |
| public static void | installProperty(JComponent
target component to set the property on c, String name of the property to set propertyName, Object value of the property propertyValue)Convenience method for installing a property with the specified name and value on a component if that property has not already been set by the developer. |
| public abstract boolean | Returns: true if this look and feel represents the underlying
platform look and feelIf the underlying platform has a "native" look and feel, and
this is an implementation of it, return |
| public abstract boolean | Returns: true if this is a supported look and feelReturn |
| public static void | loadKeyBindings(InputMap
InputMap to add the key-action
pairs tobindings to add to keys)retMapPopulates an |
| public static ComponentInputMap | Returns: newly created and populatedInputMapUIResourcecomponent to create the c, Object[] ComponentInputMapUIResource
withalternating pairs of keys)keystroke-action key
pairs as described in loadKeyBindingsCreates a |
| public static Object | |
| public static InputMap | Returns: newly created and populatedInputMapUIResourcealternating pairs of keys)keystroke-action key
pairs as described in loadKeyBindingsCreates an |
| public static JTextComponent. | Returns: an array ofKeyBindingsan array of keyBindingList)key-action pairsConvenience method for building an array of |
| public void | provideErrorFeedback(Component
the component)Component the error occurred in,
may be null
indicating the error condition is not directly
associated with a ComponentInvoked when the user attempts an invalid operation,
such as pasting into an uneditable |
| public String | Returns: a String representation of this objectOverrides java. Returns a string that displays and identifies this object's properties. |
| public void | |
| public static void | uninstallBorder(JComponent
component to uninstall the border on c)Convenience method for uninstalling a border. |
| LookAndFeel | back to summary |
|---|---|
| protected LookAndFeel() Constructor for subclasses to call. | |
| getDefaults | back to summary |
|---|---|
| public UIDefaults getDefaults() Returns the look and feel defaults. While this method is public,
it should only be invoked by the
| |
| getDescription | back to summary |
|---|---|
| public abstract String getDescription() Return a one line description of this look and feel implementation, e.g. "The CDE/Motif Look and Feel". This string is intended for the user, e.g. in the title of a window or in a ToolTip message.
| |
| getDesktopPropertyValue | back to summary |
|---|---|
| public static Object getDesktopPropertyValue(String systemPropertyName, Object fallbackValue) Returns the value of the specified system desktop property by
invoking
| |
| getDisabledIcon | back to summary |
|---|---|
| public Icon getDisabledIcon(JComponent component, Icon icon) Returns an
Some look and feels might not render the disabled
| |
| getDisabledSelectedIcon | back to summary |
|---|---|
| public Icon getDisabledSelectedIcon(JComponent component, Icon icon) Returns an
Some look and feels might not render the disabled and selected
| |
| getID | back to summary |
|---|---|
| public abstract String getID() Return a string that identifies this look and feel. This string will be used by applications/services that want to recognize well known look and feel implementations. Presently the well known names are "Motif", "Windows", "Mac", "Metal". Note that a LookAndFeel derived from a well known superclass that doesn't make any fundamental changes to the look or feel shouldn't override this method.
| |
| getLayoutStyle | back to summary |
|---|---|
| public LayoutStyle getLayoutStyle() Returns the
You generally don't use the
| |
| getName | back to summary |
|---|---|
| public abstract String getName() Return a short string that identifies this look and feel, e.g. "CDE/Motif". This string should be appropriate for a menu item. Distinct look and feels should have different names, e.g. a subclass of MotifLookAndFeel that changes the way a few components are rendered should be called "CDE/Motif My Way"; something that would be useful to a user trying to select a L&F from a list of names.
| |
| getSupportsWindowDecorations | back to summary |
|---|---|
| public boolean getSupportsWindowDecorations() Returns
The default implementation returns
| |
| initialize | back to summary |
|---|---|
| public void initialize() Initializes the look and feel. While this method is public,
it should only be invoked by the
| |
| installBorder | back to summary |
|---|---|
| public static void installBorder(JComponent c, String defaultBorderName) Convenience method for setting a component's border property with
a value from the defaults. The border is only set if the border is
| |
| installColors | back to summary |
|---|---|
| public static void installColors(JComponent c, String defaultBgName, String defaultFgName) Convenience method for setting a component's foreground
and background color properties with values from the
defaults. The properties are only set if the current
value is either
| |
| installColorsAndFont | back to summary |
|---|---|
| public static void installColorsAndFont(JComponent c, String defaultBgName, String defaultFgName, String defaultFontName) Convenience method for setting a component's foreground,
background and font properties with values from the
defaults. The properties are only set if the current
value is either
| |
| installProperty | back to summary |
|---|---|
| public static void installProperty(JComponent c, String propertyName, Object propertyValue) Convenience method for installing a property with the specified name
and value on a component if that property has not already been set
by the developer. This method is intended to be used by
ui delegate instances that need to specify a default value for a
property of primitive type (boolean, int, ..), but do not wish
to override a value set by the client. Since primitive property
values cannot be wrapped with the
| |
| isNativeLookAndFeel | back to summary |
|---|---|
| public abstract boolean isNativeLookAndFeel() If the underlying platform has a "native" look and feel, and
this is an implementation of it, return
| |
| isSupportedLookAndFeel | back to summary |
|---|---|
| public abstract boolean isSupportedLookAndFeel() Return
| |
| loadKeyBindings | back to summary |
|---|---|
| public static void loadKeyBindings(InputMap retMap, Object[] keys) Populates an
The following illustrates loading an
LookAndFeel.loadKeyBindings(inputMap, new Object[] {
"control X", "cut",
"control V", "paste"
});
Supplying a
Specifying a
| |
| makeComponentInputMap | back to summary |
|---|---|
| public static ComponentInputMap makeComponentInputMap(JComponent c, Object[] keys) Creates a
| |
| makeIcon | back to summary |
|---|---|
| public static Object makeIcon(final Class<?> baseClass, final String gifFile) Creates and returns a
This method does not check the arguments in any way. It is
strongly recommended that
| |
| makeInputMap | back to summary |
|---|---|
| public static InputMap makeInputMap(Object[] keys) Creates an
| |
| makeKeyBindings | back to summary |
|---|---|
| public static JTextComponent. Convenience method for building an array of
This method returns an array of
The following example illustrates creating a
JTextComponent.KeyBinding[] multilineBindings = makeKeyBindings( new Object[] {
"UP", DefaultEditorKit.upAction,
"DOWN", DefaultEditorKit.downAction,
"PAGE_UP", DefaultEditorKit.pageUpAction,
"PAGE_DOWN", DefaultEditorKit.pageDownAction,
"ENTER", DefaultEditorKit.insertBreakAction,
"TAB", DefaultEditorKit.insertTabAction
});
If keyBindingList's length is odd, the last element is
ignored.
Supplying a
| |
| provideErrorFeedback | back to summary |
|---|---|
| public void provideErrorFeedback(Component component) Invoked when the user attempts an invalid operation,
such as pasting into an uneditable
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Returns a string that displays and identifies this object's properties.
| |
| uninitialize | back to summary |
|---|---|
| public void uninitialize() Uninitializes the look and feel. While this method is public,
it should only be invoked by the Subclasses may choose to free up some resources here.
| |
| uninstallBorder | back to summary |
|---|---|
| public static void uninstallBorder(JComponent c) Convenience method for uninstalling a border. If the border of
the component is a
| |