- java.lang.Object
- 
- javax.swing.LookAndFeel
 
- 
- Direct Known Subclasses:
- BasicLookAndFeel,- MultiLookAndFeel
 
 public abstract class LookAndFeel extends Object LookAndFeel, as the name implies, encapsulates a look and feel. Beyond installing a look and feel most developers never need to interact directly withLookAndFeel. 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 JComponentsubclass has an implementation of a specificComponentUIsubclass. TheComponentUIis often referred to as "the ui", "component ui", or "look and feel delegate". TheComponentUIsubclass is responsible for providing the look and feel specific functionality of the component. For example,JTreerequires an implementation of theComponentUIsubclassTreeUI. The implementation of the specificComponentUIsubclass is provided by theLookAndFeel. EachJComponentsubclass identifies theComponentUIsubclass it requires by way of theJComponentmethodgetUIClassID.Each LookAndFeelimplementation must provide an implementation of the appropriateComponentUIsubclass by specifying a value for each of Swing's ui class ids in theUIDefaultsobject returned fromgetDefaults. For example,BasicLookAndFeelusesBasicTreeUIas the concrete implementation forTreeUI. This is accomplished byBasicLookAndFeelproviding the key-value pair"TreeUI"-"javax.swing.plaf.basic.BasicTreeUI", in theUIDefaultsreturned fromgetDefaults. Refer toUIDefaults.getUI(JComponent)for details on how the implementation of theComponentUIsubclass is obtained.When a LookAndFeelis installed theUIManagerdoes 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 theJComponentsubclass is created.Recommendations for Look and FeelsAs noted inUIManagereachLookAndFeelhas 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 and defaultsAllComponentUIstypically need to set various properties on theJComponenttheComponentUIis providing the look and feel for. This is typically done when theComponentUIis installed on theJComponent. Setting a property should only be done if the developer has not set the property. For non-primitive values it is recommended that theComponentUIonly change the property on theJComponentif the current value isnullor implementsUIResource. If the current value isnullor implementsUIResourceit indicates the property has not been set by the developer, and the ui is free to change it. For example,BasicButtonUI.installDefaultsonly changes the font on theJButtonif the return value frombutton.getFont()isnullor implementsUIResource. On the other hand ifbutton.getFont()returned anon-nullvalue that did not implementUIResourcethenBasicButtonUI.installDefaultswould not change theJButton's font.For primitive values, such as opaque, the methodinstallPropertyshould be invoked.installPropertyonly changes the corresponding property if the value has not been changed by the developer.ComponentUIimplementations should use the various install methods provided by this class as they handle the necessary checking and install the property using the recommended guidelines.ExceptionsAll of the install methods provided byLookAndFeelneed to access the defaults if the value of the property being changed isnullor aUIResource. 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 isnullor aUIResource, the defaults table is queried with the keyfontKey. All ofUIDefault'sget methods throw aNullPointerExceptionif passed innull. As such, unless otherwise noted each of the various install methods ofLookAndFeelthrow aNullPointerExceptionif the current value isnullor aUIResourceand the supplied defaults key isnull. In addition, unless otherwise specified all of theinstallmethods throw aNullPointerExceptionif anullcomponent is passed in.- Since:
- 1.2
 
- 
- 
Constructor SummaryConstructors Constructor Description LookAndFeel()
 - 
Method SummaryAll Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description UIDefaultsgetDefaults()Returns the look and feel defaults.abstract StringgetDescription()Return a one line description of this look and feel implementation, e.g.static ObjectgetDesktopPropertyValue(String systemPropertyName, Object fallbackValue)Returns the value of the specified system desktop property by invokingToolkit.getDefaultToolkit().getDesktopProperty().IcongetDisabledIcon(JComponent component, Icon icon)Returns anIconwith a disabled appearance.IcongetDisabledSelectedIcon(JComponent component, Icon icon)Returns anIconfor use by disabled components that are also selected.abstract StringgetID()Return a string that identifies this look and feel.LayoutStylegetLayoutStyle()Returns theLayoutStylefor this look and feel.abstract StringgetName()Return a short string that identifies this look and feel, e.g.booleangetSupportsWindowDecorations()Returnstrueif theLookAndFeelreturnedRootPaneUIinstances support providingWindowdecorations in aJRootPane.voidinitialize()Initializes the look and feel.static voidinstallBorder(JComponent c, String defaultBorderName)Convenience method for setting a component's border property with a value from the defaults.static voidinstallColors(JComponent c, String defaultBgName, String defaultFgName)Convenience method for setting a component's foreground and background color properties with values from the defaults.static voidinstallColorsAndFont(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.static voidinstallProperty(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.abstract booleanisNativeLookAndFeel()If the underlying platform has a "native" look and feel, and this is an implementation of it, returntrue.abstract booleanisSupportedLookAndFeel()Returntrueif the underlying platform supports and or permits this look and feel.static voidloadKeyBindings(InputMap retMap, Object[] keys)Populates anInputMapwith the specified bindings.static ComponentInputMapmakeComponentInputMap(JComponent c, Object[] keys)Creates aComponentInputMapUIResourcefromkeys.static ObjectmakeIcon(Class<?> baseClass, String gifFile)Creates and returns aUIDefault.LazyValuethat loads an image.static InputMapmakeInputMap(Object[] keys)Creates anInputMapUIResourcefromkeys.static JTextComponent.KeyBinding[]makeKeyBindings(Object[] keyBindingList)Convenience method for building an array ofKeyBindings.voidprovideErrorFeedback(Component component)Invoked when the user attempts an invalid operation, such as pasting into an uneditableJTextFieldthat has focus.StringtoString()Returns a string that displays and identifies this object's properties.voiduninitialize()Uninitializes the look and feel.static voiduninstallBorder(JComponent c)Convenience method for uninstalling a border.
 
- 
- 
- 
Method Detail- 
installColorspublic 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 eithernullor aUIResource.- Parameters:
- c- component to set the colors on
- defaultBgName- key for the background
- defaultFgName- key for the foreground
- Throws:
- NullPointerException- as described in exceptions
- See Also:
- installColorsAndFont(javax.swing.JComponent, java.lang.String, java.lang.String, java.lang.String),- UIManager.getColor(java.lang.Object)
 
 - 
installColorsAndFontpublic 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 eithernullor aUIResource.- Parameters:
- c- component set to the colors and font on
- defaultBgName- key for the background
- defaultFgName- key for the foreground
- defaultFontName- key for the font
- Throws:
- NullPointerException- as described in exceptions
- See Also:
- installColors(javax.swing.JComponent, java.lang.String, java.lang.String),- UIManager.getColor(java.lang.Object),- UIManager.getFont(java.lang.Object)
 
 - 
installBorderpublic 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 isnullor an instance ofUIResource.- Parameters:
- c- component to set the border on
- defaultBorderName- key specifying the border
- Throws:
- NullPointerException- as described in exceptions
 
 - 
uninstallBorderpublic static void uninstallBorder(JComponent c) Convenience method for uninstalling a border. If the border of the component is aUIResource, it is set tonull.- Parameters:
- c- component to uninstall the border on
- Throws:
- NullPointerException- if- cis- null
 
 - 
installPropertypublic 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 theUIResourcemarker, this method uses private state to determine whether the property has been set by the client.- Parameters:
- c- target component to set the property on
- propertyName- name of the property to set
- propertyValue- value of the property
- Throws:
- IllegalArgumentException- if the specified property is not one which can be set using this method
- ClassCastException- if the property value has not been set by the developer and the type does not match the property's type
- NullPointerException- if- cis- null, or the named property has not been set by the developer and- propertyValueis- null
- Since:
- 1.5
 
 - 
makeKeyBindingspublic static JTextComponent.KeyBinding[] makeKeyBindings(Object[] keyBindingList) Convenience method for building an array ofKeyBindings. While this method is not deprecated, developers should instead useActionMapandInputMapfor supplying key bindings.This method returns an array of KeyBindings, one for each alternatingkey-actionpair inkeyBindingList. Akeycan either be aStringin the format specified by theKeyStroke.getKeyStrokemethod, or aKeyStroke. Theactionpart of the pair is aStringthat corresponds to the name of theAction.The following example illustrates creating a KeyBindingarray from six alternatingkey-actionpairs: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 });IfkeyBindingList'slength is odd, the last element is ignored.Supplying a nullvalue for either thekeyoractionpart of thekey-actionpair results in creating aKeyBindingwith the corresponding valuenull. As other parts of Swing's expectnon-nullvalues in aKeyBinding, you should avoid supplyingnullas either thekeyoractionpart of thekey-actionpair.- Parameters:
- keyBindingList- an array of- key-actionpairs
- Returns:
- an array of KeyBindings
- Throws:
- NullPointerException- if- keyBindingListis- null
- ClassCastException- if the- keypart of the pair is not a- KeyStrokeor- String, or the- actionpart of the pair is not a- String
- See Also:
- ActionMap,- InputMap,- KeyStroke.getKeyStroke(char)
 
 - 
makeInputMappublic static InputMap makeInputMap(Object[] keys) Creates anInputMapUIResourcefromkeys. This is a convenience method for creating a newInputMapUIResource, invokingloadKeyBindings(map, keys), and returning theInputMapUIResource.- Parameters:
- keys- alternating pairs of- keystroke-action keypairs as described in- loadKeyBindings(javax.swing.InputMap, java.lang.Object[])
- Returns:
- newly created and populated InputMapUIResource
- Since:
- 1.3
- See Also:
- loadKeyBindings(javax.swing.InputMap, java.lang.Object[])
 
 - 
makeComponentInputMappublic static ComponentInputMap makeComponentInputMap(JComponent c, Object[] keys) Creates aComponentInputMapUIResourcefromkeys. This is a convenience method for creating a newComponentInputMapUIResource, invokingloadKeyBindings(map, keys), and returning theComponentInputMapUIResource.- Parameters:
- c- component to create the- ComponentInputMapUIResourcewith
- keys- alternating pairs of- keystroke-action keypairs as described in- loadKeyBindings(javax.swing.InputMap, java.lang.Object[])
- Returns:
- newly created and populated InputMapUIResource
- Throws:
- IllegalArgumentException- if- cis- null
- Since:
- 1.3
- See Also:
- loadKeyBindings(javax.swing.InputMap, java.lang.Object[]),- ComponentInputMapUIResource
 
 - 
loadKeyBindingspublic static void loadKeyBindings(InputMap retMap, Object[] keys) Populates anInputMapwith the specified bindings. The bindings are supplied as a list of alternatingkeystroke-action keypairs. Thekeystrokeis either an instance ofKeyStroke, or aStringthat identifies theKeyStrokefor the binding. Refer toKeyStroke.getKeyStroke(String)for the specific format. Theaction keypart of the pair is the key registered in theInputMapfor theKeyStroke.The following illustrates loading an InputMapwith twokey-actionpairs:LookAndFeel.loadKeyBindings(inputMap, new Object[] { "control X", "cut", "control V", "paste" });Supplying a nulllist of bindings (keys) does not changeretMapin any way.Specifying a nullaction keyresults in removing thekeystroke'sentry from theInputMap. Anullkeystrokeis ignored.- Parameters:
- retMap-- InputMapto add the- key-actionpairs to
- keys- bindings to add to- retMap
- Throws:
- NullPointerException- if- keysis- non-null, not empty, and- retMapis- null
- Since:
- 1.3
- See Also:
- KeyStroke.getKeyStroke(String),- InputMap
 
 - 
makeIconpublic static Object makeIcon(Class<?> baseClass, String gifFile) Creates and returns aUIDefault.LazyValuethat loads an image. The returned value is an implementation ofUIDefaults.LazyValue. WhencreateValueis invoked on the returned object, the image is loaded. If the image isnon-null, it is then wrapped in anIconthat implementsUIResource. The image is loaded usingClass.getResourceAsStream(gifFile).This method does not check the arguments in any way. It is strongly recommended that non-nullvalues are supplied else exceptions may occur whencreateValueis invoked on the returned object.- Parameters:
- baseClass-- Classused to load the resource
- gifFile- path to the image to load
- Returns:
- a UIDefaults.LazyValue; when resolved theLazyValueloads the specified image
- See Also:
- UIDefaults.LazyValue,- Icon,- Class.getResourceAsStream(String)
 
 - 
getLayoutStylepublic LayoutStyle getLayoutStyle() Returns theLayoutStylefor this look and feel. This never returnsnull.You generally don't use the LayoutStylefrom the look and feel, instead use theLayoutStylemethodgetInstance.- Returns:
- the LayoutStylefor this look and feel
- Since:
- 1.6
- See Also:
- LayoutStyle.getInstance()
 
 - 
provideErrorFeedbackpublic void provideErrorFeedback(Component component) Invoked when the user attempts an invalid operation, such as pasting into an uneditableJTextFieldthat has focus. The default implementation beeps. Subclasses that wish different behavior should override this and provide the additional feedback.- Parameters:
- component- the- Componentthe error occurred in, may be- nullindicating the error condition is not directly associated with a- Component
- Since:
- 1.4
 
 - 
getDesktopPropertyValuepublic static Object getDesktopPropertyValue(String systemPropertyName, Object fallbackValue) Returns the value of the specified system desktop property by invokingToolkit.getDefaultToolkit().getDesktopProperty(). If the value of the specified property isnull,fallbackValueis returned.- Parameters:
- systemPropertyName- the name of the system desktop property being queried
- fallbackValue- the object to be returned as the value if the system value is null
- Returns:
- the current value of the desktop property
- Since:
- 1.4
- See Also:
- Toolkit.getDesktopProperty(java.lang.String)
 
 - 
getDisabledIconpublic Icon getDisabledIcon(JComponent component, Icon icon) Returns anIconwith a disabled appearance. This method is used to generate a disabledIconwhen one has not been specified. For example, if you create aJButtonand only specify anIconviasetIconthis method will be called to generate the disabledIcon. Ifnullis passed asiconthis method returnsnull.Some look and feels might not render the disabled Icon, in which case they will ignore this.- Parameters:
- component-- JComponentthat will display the- Icon, may be- null
- icon-- Iconto generate the disabled icon from
- Returns:
- disabled Icon, ornullif a suitableIconcan not be generated
- Since:
- 1.5
 
 - 
getDisabledSelectedIconpublic Icon getDisabledSelectedIcon(JComponent component, Icon icon) Returns anIconfor use by disabled components that are also selected. This method is used to generate anIconfor components that are in both the disabled and selected states but do not have a specificIconfor this state. For example, if you create aJButtonand only specify anIconviasetIconthis method will be called to generate the disabled and selectedIcon. Ifnullis passed asiconthis methods returnsnull.Some look and feels might not render the disabled and selected Icon, in which case they will ignore this.- Parameters:
- component-- JComponentthat will display the- Icon, may be- null
- icon-- Iconto generate disabled and selected icon from
- Returns:
- disabled and selected icon, or nullif a suitableIconcan not be generated.
- Since:
- 1.5
 
 - 
getNamepublic 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.- Returns:
- short identifier for the look and feel
 
 - 
getIDpublic 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.- Returns:
- identifier for the look and feel
 
 - 
getDescriptionpublic 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.- Returns:
- short description for the look and feel
 
 - 
getSupportsWindowDecorationspublic boolean getSupportsWindowDecorations() Returnstrueif theLookAndFeelreturnedRootPaneUIinstances support providingWindowdecorations in aJRootPane.The default implementation returns false, subclasses that supportWindowdecorations should override this and returntrue.- Returns:
- trueif the- RootPaneUIinstances created by this look and feel support client side decorations
- Since:
- 1.4
- See Also:
- JDialog.setDefaultLookAndFeelDecorated(boolean),- JFrame.setDefaultLookAndFeelDecorated(boolean),- JRootPane.setWindowDecorationStyle(int)
 
 - 
isNativeLookAndFeelpublic abstract boolean isNativeLookAndFeel() If the underlying platform has a "native" look and feel, and this is an implementation of it, returntrue. For example, when the underlying platform is Solaris running CDE a CDE/Motif look and feel implementation would returntrue.- Returns:
- trueif this look and feel represents the underlying platform look and feel
 
 - 
isSupportedLookAndFeelpublic abstract boolean isSupportedLookAndFeel() Returntrueif the underlying platform supports and or permits this look and feel. This method returnsfalseif the look and feel depends on special resources or legal agreements that aren't defined for the current platform.- Returns:
- trueif this is a supported look and feel
- See Also:
- UIManager.setLookAndFeel(javax.swing.LookAndFeel)
 
 - 
initializepublic void initialize() Initializes the look and feel. While this method is public, it should only be invoked by theUIManagerwhen a look and feel is installed as the current look and feel. This method is invoked before theUIManagerinvokesgetDefaults. This method is intended to perform any initialization for the look and feel. Subclasses should do any one-time setup they need here, rather than in a static initializer, because look and feel class objects may be loaded just to discover thatisSupportedLookAndFeel()returnsfalse.
 - 
uninitializepublic void uninitialize() Uninitializes the look and feel. While this method is public, it should only be invoked by theUIManagerwhen the look and feel is uninstalled. For example,UIManager.setLookAndFeelinvokes this when the look and feel is changed.Subclasses may choose to free up some resources here. 
 - 
getDefaultspublic UIDefaults getDefaults() Returns the look and feel defaults. While this method is public, it should only be invoked by theUIManagerwhen the look and feel is set as the current look and feel and afterinitializehas been invoked.- Returns:
- the look and feel defaults
- See Also:
- initialize(),- uninitialize(),- UIManager.setLookAndFeel(javax.swing.LookAndFeel)
 
 
- 
 
-