- 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
JComponent
subclass has an implementation of a specificComponentUI
subclass. TheComponentUI
is often referred to as "the ui", "component ui", or "look and feel delegate". TheComponentUI
subclass is responsible for providing the look and feel specific functionality of the component. For example,JTree
requires an implementation of theComponentUI
subclassTreeUI
. The implementation of the specificComponentUI
subclass is provided by theLookAndFeel
. EachJComponent
subclass identifies theComponentUI
subclass it requires by way of theJComponent
methodgetUIClassID
.Each
LookAndFeel
implementation must provide an implementation of the appropriateComponentUI
subclass by specifying a value for each of Swing's ui class ids in theUIDefaults
object returned fromgetDefaults
. For example,BasicLookAndFeel
usesBasicTreeUI
as the concrete implementation forTreeUI
. This is accomplished byBasicLookAndFeel
providing the key-value pair"TreeUI"-"javax.swing.plaf.basic.BasicTreeUI"
, in theUIDefaults
returned fromgetDefaults
. Refer toUIDefaults.getUI(JComponent)
for details on how the implementation of theComponentUI
subclass is obtained.When a
LookAndFeel
is installed theUIManager
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 theJComponent
subclass is created.Recommendations for Look and Feels
As noted inUIManager
eachLookAndFeel
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 and defaults
AllComponentUIs
typically need to set various properties on theJComponent
theComponentUI
is providing the look and feel for. This is typically done when theComponentUI
is 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 theComponentUI
only change the property on theJComponent
if the current value isnull
or implementsUIResource
. If the current value isnull
or implementsUIResource
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 theJButton
if the return value frombutton.getFont()
isnull
or implementsUIResource
. On the other hand ifbutton.getFont()
returned anon-null
value that did not implementUIResource
thenBasicButtonUI.installDefaults
would not change theJButton
's font.For primitive values, such as
opaque
, the methodinstallProperty
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.Exceptions
All of the install methods provided byLookAndFeel
need to access the defaults if the value of the property being changed isnull
or 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 isnull
or aUIResource
, the defaults table is queried with the keyfontKey
. All ofUIDefault's
get methods throw aNullPointerException
if passed innull
. As such, unless otherwise noted each of the various install methods ofLookAndFeel
throw aNullPointerException
if the current value isnull
or aUIResource
and the supplied defaults key isnull
. In addition, unless otherwise specified all of theinstall
methods throw aNullPointerException
if anull
component is passed in.- Since:
- 1.2
-
-
Constructor Summary
Constructors Constructor Description LookAndFeel()
-
Method Summary
Modifier and Type Method Description UIDefaults
getDefaults()
Returns the look and feel defaults.abstract String
getDescription()
Return a one line description of this look and feel implementation, e.g.static Object
getDesktopPropertyValue(String systemPropertyName, Object fallbackValue)
Returns the value of the specified system desktop property by invokingToolkit.getDefaultToolkit().getDesktopProperty()
.Icon
getDisabledIcon(JComponent component, Icon icon)
Returns anIcon
with a disabled appearance.Icon
getDisabledSelectedIcon(JComponent component, Icon icon)
Returns anIcon
for use by disabled components that are also selected.abstract String
getID()
Return a string that identifies this look and feel.LayoutStyle
getLayoutStyle()
Returns theLayoutStyle
for this look and feel.abstract String
getName()
Return a short string that identifies this look and feel, e.g.boolean
getSupportsWindowDecorations()
Returnstrue
if theLookAndFeel
returnedRootPaneUI
instances support providingWindow
decorations in aJRootPane
.void
initialize()
Initializes the look and feel.static void
installBorder(JComponent c, String defaultBorderName)
Convenience method for setting a component's border property with a value from the defaults.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.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.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.abstract boolean
isNativeLookAndFeel()
If the underlying platform has a "native" look and feel, and this is an implementation of it, returntrue
.abstract boolean
isSupportedLookAndFeel()
Returntrue
if the underlying platform supports and or permits this look and feel.static void
loadKeyBindings(InputMap retMap, Object[] keys)
Populates anInputMap
with the specified bindings.static ComponentInputMap
makeComponentInputMap(JComponent c, Object[] keys)
Creates aComponentInputMapUIResource
fromkeys
.static Object
makeIcon(Class<?> baseClass, String gifFile)
Creates and returns aUIDefault.LazyValue
that loads an image.static InputMap
makeInputMap(Object[] keys)
Creates anInputMapUIResource
fromkeys
.static JTextComponent.KeyBinding[]
makeKeyBindings(Object[] keyBindingList)
Convenience method for building an array ofKeyBindings
.void
provideErrorFeedback(Component component)
Invoked when the user attempts an invalid operation, such as pasting into an uneditableJTextField
that has focus.String
toString()
Returns a string that displays and identifies this object's properties.void
uninitialize()
Uninitializes the look and feel.static void
uninstallBorder(JComponent c)
Convenience method for uninstalling a border.
-
-
-
Method Detail
-
installColors
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 eithernull
or aUIResource
.- Parameters:
c
- component to set the colors ondefaultBgName
- key for the backgrounddefaultFgName
- 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)
-
installColorsAndFont
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 eithernull
or aUIResource
.- Parameters:
c
- component set to the colors and font ondefaultBgName
- key for the backgrounddefaultFgName
- key for the foregrounddefaultFontName
- 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)
-
installBorder
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 isnull
or an instance ofUIResource
.- Parameters:
c
- component to set the border ondefaultBorderName
- key specifying the border- Throws:
NullPointerException
- as described in exceptions
-
uninstallBorder
public 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
- ifc
isnull
-
installProperty
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 theUIResource
marker, this method uses private state to determine whether the property has been set by the client.- Parameters:
c
- target component to set the property onpropertyName
- name of the property to setpropertyValue
- value of the property- Throws:
IllegalArgumentException
- if the specified property is not one which can be set using this methodClassCastException
- if the property value has not been set by the developer and the type does not match the property's typeNullPointerException
- ifc
isnull
, or the named property has not been set by the developer andpropertyValue
isnull
- Since:
- 1.5
-
makeKeyBindings
public static JTextComponent.KeyBinding[] makeKeyBindings(Object[] keyBindingList)
Convenience method for building an array ofKeyBindings
. While this method is not deprecated, developers should instead useActionMap
andInputMap
for supplying key bindings.This method returns an array of
KeyBindings
, one for each alternatingkey-action
pair inkeyBindingList
. Akey
can either be aString
in the format specified by theKeyStroke.getKeyStroke
method, or aKeyStroke
. Theaction
part of the pair is aString
that corresponds to the name of theAction
.The following example illustrates creating a
KeyBinding
array from six alternatingkey-action
pairs: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's
length is odd, the last element is ignored.Supplying a
null
value for either thekey
oraction
part of thekey-action
pair results in creating aKeyBinding
with the corresponding valuenull
. As other parts of Swing's expectnon-null
values in aKeyBinding
, you should avoid supplyingnull
as either thekey
oraction
part of thekey-action
pair.- Parameters:
keyBindingList
- an array ofkey-action
pairs- Returns:
- an array of
KeyBindings
- Throws:
NullPointerException
- ifkeyBindingList
isnull
ClassCastException
- if thekey
part of the pair is not aKeyStroke
orString
, or theaction
part of the pair is not aString
- See Also:
ActionMap
,InputMap
,KeyStroke.getKeyStroke(char)
-
makeInputMap
public static InputMap makeInputMap(Object[] keys)
Creates anInputMapUIResource
fromkeys
. This is a convenience method for creating a newInputMapUIResource
, invokingloadKeyBindings(map, keys)
, and returning theInputMapUIResource
.- Parameters:
keys
- alternating pairs ofkeystroke-action key
pairs as described inloadKeyBindings(javax.swing.InputMap, java.lang.Object[])
- Returns:
- newly created and populated
InputMapUIResource
- Since:
- 1.3
- See Also:
loadKeyBindings(javax.swing.InputMap, java.lang.Object[])
-
makeComponentInputMap
public static ComponentInputMap makeComponentInputMap(JComponent c, Object[] keys)
Creates aComponentInputMapUIResource
fromkeys
. This is a convenience method for creating a newComponentInputMapUIResource
, invokingloadKeyBindings(map, keys)
, and returning theComponentInputMapUIResource
.- Parameters:
c
- component to create theComponentInputMapUIResource
withkeys
- alternating pairs ofkeystroke-action key
pairs as described inloadKeyBindings(javax.swing.InputMap, java.lang.Object[])
- Returns:
- newly created and populated
InputMapUIResource
- Throws:
IllegalArgumentException
- ifc
isnull
- Since:
- 1.3
- See Also:
loadKeyBindings(javax.swing.InputMap, java.lang.Object[])
,ComponentInputMapUIResource
-
loadKeyBindings
public static void loadKeyBindings(InputMap retMap, Object[] keys)
Populates anInputMap
with the specified bindings. The bindings are supplied as a list of alternatingkeystroke-action key
pairs. Thekeystroke
is either an instance ofKeyStroke
, or aString
that identifies theKeyStroke
for the binding. Refer toKeyStroke.getKeyStroke(String)
for the specific format. Theaction key
part of the pair is the key registered in theInputMap
for theKeyStroke
.The following illustrates loading an
InputMap
with twokey-action
pairs:LookAndFeel.loadKeyBindings(inputMap, new Object[] { "control X", "cut", "control V", "paste" });
Supplying a
null
list of bindings (keys
) does not changeretMap
in any way.Specifying a
null
action key
results in removing thekeystroke's
entry from theInputMap
. Anull
keystroke
is ignored.- Parameters:
retMap
-InputMap
to add thekey-action
pairs tokeys
- bindings to add toretMap
- Throws:
NullPointerException
- ifkeys
isnon-null
, not empty, andretMap
isnull
- Since:
- 1.3
- See Also:
KeyStroke.getKeyStroke(String)
,InputMap
-
makeIcon
public static Object makeIcon(Class<?> baseClass, String gifFile)
Creates and returns aUIDefault.LazyValue
that loads an image. The returned value is an implementation ofUIDefaults.LazyValue
. WhencreateValue
is invoked on the returned object, the image is loaded. If the image isnon-null
, it is then wrapped in anIcon
that implementsUIResource
. The image is loaded usingClass.getResourceAsStream(gifFile)
.This method does not check the arguments in any way. It is strongly recommended that
non-null
values are supplied else exceptions may occur whencreateValue
is invoked on the returned object.- Parameters:
baseClass
-Class
used to load the resourcegifFile
- path to the image to load- Returns:
- a
UIDefaults.LazyValue
; when resolved theLazyValue
loads the specified image - See Also:
UIDefaults.LazyValue
,Icon
,Class.getResourceAsStream(String)
-
getLayoutStyle
public LayoutStyle getLayoutStyle()
Returns theLayoutStyle
for this look and feel. This never returnsnull
.You generally don't use the
LayoutStyle
from the look and feel, instead use theLayoutStyle
methodgetInstance
.- Returns:
- the
LayoutStyle
for this look and feel - Since:
- 1.6
- See Also:
LayoutStyle.getInstance()
-
provideErrorFeedback
public void provideErrorFeedback(Component component)
Invoked when the user attempts an invalid operation, such as pasting into an uneditableJTextField
that has focus. The default implementation beeps. Subclasses that wish different behavior should override this and provide the additional feedback.- Parameters:
component
- theComponent
the error occurred in, may benull
indicating the error condition is not directly associated with aComponent
- Since:
- 1.4
-
getDesktopPropertyValue
public 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
,fallbackValue
is returned.- Parameters:
systemPropertyName
- the name of the system desktop property being queriedfallbackValue
- 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)
-
getDisabledIcon
public Icon getDisabledIcon(JComponent component, Icon icon)
Returns anIcon
with a disabled appearance. This method is used to generate a disabledIcon
when one has not been specified. For example, if you create aJButton
and only specify anIcon
viasetIcon
this method will be called to generate the disabledIcon
. Ifnull
is passed asicon
this method returnsnull
.Some look and feels might not render the disabled
Icon
, in which case they will ignore this.- Parameters:
component
-JComponent
that will display theIcon
, may benull
icon
-Icon
to generate the disabled icon from- Returns:
- disabled
Icon
, ornull
if a suitableIcon
can not be generated - Since:
- 1.5
-
getDisabledSelectedIcon
public Icon getDisabledSelectedIcon(JComponent component, Icon icon)
Returns anIcon
for use by disabled components that are also selected. This method is used to generate anIcon
for components that are in both the disabled and selected states but do not have a specificIcon
for this state. For example, if you create aJButton
and only specify anIcon
viasetIcon
this method will be called to generate the disabled and selectedIcon
. Ifnull
is passed asicon
this methods returnsnull
.Some look and feels might not render the disabled and selected
Icon
, in which case they will ignore this.- Parameters:
component
-JComponent
that will display theIcon
, may benull
icon
-Icon
to generate disabled and selected icon from- Returns:
- disabled and selected icon, or
null
if a suitableIcon
can not be generated. - Since:
- 1.5
-
getName
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.- Returns:
- short identifier for the look and feel
-
getID
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.- Returns:
- identifier for the look and feel
-
getDescription
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.- Returns:
- short description for the look and feel
-
getSupportsWindowDecorations
public boolean getSupportsWindowDecorations()
Returnstrue
if theLookAndFeel
returnedRootPaneUI
instances support providingWindow
decorations in aJRootPane
.The default implementation returns
false
, subclasses that supportWindow
decorations should override this and returntrue
.- Returns:
true
if theRootPaneUI
instances created by this look and feel support client side decorations- Since:
- 1.4
- See Also:
JDialog.setDefaultLookAndFeelDecorated(boolean)
,JFrame.setDefaultLookAndFeelDecorated(boolean)
,JRootPane.setWindowDecorationStyle(int)
-
isNativeLookAndFeel
public 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:
true
if this look and feel represents the underlying platform look and feel
-
isSupportedLookAndFeel
public abstract boolean isSupportedLookAndFeel()
Returntrue
if the underlying platform supports and or permits this look and feel. This method returnsfalse
if the look and feel depends on special resources or legal agreements that aren't defined for the current platform.- Returns:
true
if this is a supported look and feel- See Also:
UIManager.setLookAndFeel(javax.swing.LookAndFeel)
-
initialize
public void initialize()
Initializes the look and feel. While this method is public, it should only be invoked by theUIManager
when a look and feel is installed as the current look and feel. This method is invoked before theUIManager
invokesgetDefaults
. 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
.
-
uninitialize
public void uninitialize()
Uninitializes the look and feel. While this method is public, it should only be invoked by theUIManager
when the look and feel is uninstalled. For example,UIManager.setLookAndFeel
invokes this when the look and feel is changed.Subclasses may choose to free up some resources here.
-
getDefaults
public UIDefaults getDefaults()
Returns the look and feel defaults. While this method is public, it should only be invoked by theUIManager
when the look and feel is set as the current look and feel and afterinitialize
has been invoked.- Returns:
- the look and feel defaults
- See Also:
initialize()
,uninitialize()
,UIManager.setLookAndFeel(javax.swing.LookAndFeel)
-
-