javax.swing

Class SwingUtilities

public class SwingUtilities extends Object implements SwingConstants

A number of static utility functions which are useful when drawing swing components, dispatching events, or calculating regions which need painting.
Method Summary
static RectanglecalculateInnerArea(JComponent c, Rectangle r)
Calculates the portion of the component's bounds which is inside the component's border insets.
static Rectangle[]computeDifference(Rectangle rectA, Rectangle rectB)
Subtracts a rectangle from another and return the area as an array of rectangles.
static RectanglecomputeIntersection(int x, int y, int w, int h, Rectangle rect)
Calculates the intersection of two rectangles.
static intcomputeStringWidth(FontMetrics fm, String str)
Calculates the width of a given string.
static RectanglecomputeUnion(int x, int y, int w, int h, Rectangle rect)
Calculates the union of two rectangles.
static MouseEventconvertMouseEvent(Component source, MouseEvent sourceEvent, Component destination)
Convert a mouse event which refrers to one component to another.
static PointconvertPoint(Component source, int x, int y, Component destination)
Converts a point (x,y) from the coordinate space of one component to another.
static PointconvertPoint(Component source, Point aPoint, Component destination)
static voidconvertPointFromScreen(Point p, Component c)
Converts a point from "screen" coordinates (such as the coordinate space mouse events are delivered in) to a component's local coordinate space.
static voidconvertPointToScreen(Point p, Component c)
Converts a point from a component's local coordinate space to "screen" coordinates (such as the coordinate space mouse events are delivered in).
static RectangleconvertRectangle(Component source, Rectangle rect, Component destination)
Converts a rectangle from the coordinate space of one component to another.
static ComponentfindFocusOwner(Component comp)
Returns the focus owner or null if comp is not the focus owner or a parent of it.
static AccessiblegetAccessibleAt(Component c, Point p)
Returns the Accessible child of the specified component which appears at the supplied Point.
static AccessiblegetAccessibleChild(Component c, int i)

Returns the Accessible child of the specified component that has the supplied index within the parent component.

static intgetAccessibleChildrenCount(Component c)

Returns the number of Accessible children within the supplied component.

static intgetAccessibleIndexInParent(Component c)

Returns the zero-based index of the specified component within its parent.

static AccessibleStateSetgetAccessibleStateSet(Component c)

Returns a set of AccessibleStates, which represent the state of the supplied component.

static ContainergetAncestorNamed(String name, Component comp)
Returns the least ancestor of comp which has the specified name.
static ContainergetAncestorOfClass(Class<?> c, Component comp)
Returns the least ancestor of comp which is an instance of the specified class.
static ComponentgetDeepestComponentAt(Component parent, int x, int y)
Returns the deepest descendent of parent which is both visible and contains the point (x,y).
static RectanglegetLocalBounds(Component aComponent)
Calculates the bounds of a component in the component's own coordinate space.
static ComponentgetRoot(Component comp)
Returns the "root" of the component tree containint comp The root is defined as either the least ancestor of comp which is a {@link Window}, or the greatest ancestor of comp which is a {@link Applet} if no {@link Window} ancestors are found.
static JRootPanegetRootPane(Component comp)
If comp is a RootPaneContainer, return its JRootPane.
static ActionMapgetUIActionMap(JComponent component)
Returns the ActionMap that is provided by the ComponentUI of component.
static InputMapgetUIInputMap(JComponent component, int cond)
Returns the InputMap that is provided by the ComponentUI of component for the specified condition.
static WindowgetWindowAncestor(Component comp)
Returns the first ancestor of comp that is a {@link Window} or null if comp is not contained in a {@link Window}.
static voidinvokeAndWait(Runnable doRun)
Calls {@link java.awt.EventQueue#invokeAndWait} with the specified {@link Runnable}.
static voidinvokeLater(Runnable doRun)
Calls {@link java.awt.EventQueue#invokeLater} with the specified {@link Runnable}.
static booleanisDescendingFrom(Component a, Component b)
Return true if a descends from b, in other words if b is an ancestor of a.
static booleanisEventDispatchThread()
Calls {@link java.awt.EventQueue#isDispatchThread()}.
static booleanisLeftMouseButton(MouseEvent event)
Checks if left mouse button was clicked.
static booleanisMiddleMouseButton(MouseEvent event)
Checks if middle mouse button was clicked.
static booleanisRectangleContainingRectangle(Rectangle a, Rectangle b)
Tests if a rectangle contains another.
static booleanisRightMouseButton(MouseEvent event)
Checks if right mouse button was clicked.
static StringlayoutCompoundLabel(JComponent c, FontMetrics fm, String text, Icon icon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewR, Rectangle iconR, Rectangle textR, int textIconGap)

Layout a "compound label" consisting of a text string and an icon which is to be placed near the rendered text.

static StringlayoutCompoundLabel(FontMetrics fm, String text, Icon icon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewR, Rectangle iconR, Rectangle textR, int textIconGap)

Layout a "compound label" consisting of a text string and an icon which is to be placed near the rendered text.

static booleannotifyAction(Action action, KeyStroke ks, KeyEvent event, Object sender, int modifiers)
static voidpaintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h)
This method paints the given component at the given position and size.
static voidpaintComponent(Graphics g, Component c, Container p, Rectangle r)
This method paints the given component in the given rectangle.
static booleanprocessKeyBindings(KeyEvent ev)
Processes key bindings for the component that is associated with the key event.
static voidreplaceUIActionMap(JComponent component, ActionMap uiActionMap)

Change the shared, UI-managed {@link ActionMap} for a given component.

static voidreplaceUIInputMap(JComponent component, int condition, InputMap uiInputMap)

Change the shared, UI-managed {@link InputMap} for a given component.

static voidupdateComponentTreeUI(Component comp)
Recursively walk the component tree under comp calling updateUI on each {@link JComponent} found.
static WindowwindowForComponent(Component comp)
Equivalent to calling getAncestorOfClass(Window, comp).

Method Detail

calculateInnerArea

public static Rectangle calculateInnerArea(JComponent c, Rectangle r)
Calculates the portion of the component's bounds which is inside the component's border insets. This area is usually the area a component should confine its painting to. The coordinates are returned in terms of the component's coordinate system, where (0,0) is the upper left corner of the component's bounds.

Parameters: c the component to measure the bounds of (if null, this method returns null). r a carrier to store the return value in (if null, a new Rectangle instance is created).

Returns: The calculated area inside the component and its border insets.

computeDifference

public static Rectangle[] computeDifference(Rectangle rectA, Rectangle rectB)
Subtracts a rectangle from another and return the area as an array of rectangles. Returns the areas of rectA which are not covered by rectB. If the rectangles do not overlap, or if either parameter is null, a zero-size array is returned.

Parameters: rectA The first rectangle rectB The rectangle to subtract from the first

Returns: An array of rectangles representing the area in rectA not overlapped by rectB

computeIntersection

public static Rectangle computeIntersection(int x, int y, int w, int h, Rectangle rect)
Calculates the intersection of two rectangles. The result is stored in rect. This is basically the same like {@link Rectangle#intersection(Rectangle)}, only that it does not create new Rectangle instances. The tradeoff is that you loose any data in rect.

Parameters: x upper-left x coodinate of first rectangle y upper-left y coodinate of first rectangle w width of first rectangle h height of first rectangle rect a Rectangle object of the second rectangle

Returns: a rectangle corresponding to the intersection of the two rectangles. An empty rectangle is returned if the rectangles do not overlap

Throws: NullPointerException if rect is null

computeStringWidth

public static int computeStringWidth(FontMetrics fm, String str)
Calculates the width of a given string.

Parameters: fm the FontMetrics object to use str the string

Returns: the width of the the string.

computeUnion

public static Rectangle computeUnion(int x, int y, int w, int h, Rectangle rect)
Calculates the union of two rectangles. The result is stored in rect. This is basically the same as {@link Rectangle#union(Rectangle)} except that it avoids creation of new Rectangle objects. The tradeoff is that you loose any data in rect.

Parameters: x upper-left x coodinate of first rectangle y upper-left y coodinate of first rectangle w width of first rectangle h height of first rectangle rect a Rectangle object of the second rectangle

Returns: a rectangle corresponding to the union of the two rectangles; a rectangle encompassing both is returned if the rectangles do not overlap

Throws: NullPointerException if rect is null

convertMouseEvent

public static MouseEvent convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination)
Convert a mouse event which refrers to one component to another. This includes changing the mouse event's coordinate space, as well as the source property of the event. If source is null, it is taken to refer to destination's root component. If destination is null, the new event will remain expressed in source's coordinate system.

Parameters: source The component the mouse event currently refers to sourceEvent The mouse event to convert destination The component the new mouse event should refer to

Returns: A new mouse event expressed in terms of the destination component's coordinate space, and with the destination component as its source

See Also: SwingUtilities

convertPoint

public static Point convertPoint(Component source, int x, int y, Component destination)
Converts a point (x,y) from the coordinate space of one component to another. This is equivalent to converting the point from source space to screen space, then back from screen space to destination space. If exactly one of the two Components is null, it is taken to refer to the root ancestor of the other component. If both are null, no transformation is done.

Parameters: source The component which the point is expressed in terms of x Horizontal coordinate of point to transform y Vertical coordinate of point to transform destination The component which the return value will be expressed in terms of

Returns: The point (x,y) converted from the coordinate space of the source component to the coordinate space of the destination component

See Also: SwingUtilities SwingUtilities SwingUtilities SwingUtilities

convertPoint

public static Point convertPoint(Component source, Point aPoint, Component destination)

convertPointFromScreen

public static void convertPointFromScreen(Point p, Component c)
Converts a point from "screen" coordinates (such as the coordinate space mouse events are delivered in) to a component's local coordinate space. This operation is equivalent to translating the point by the negation of the component's location (which is the origin of its coordinate space).

Parameters: p The point to convert c The component which the point should be expressed in terms of

convertPointToScreen

public static void convertPointToScreen(Point p, Component c)
Converts a point from a component's local coordinate space to "screen" coordinates (such as the coordinate space mouse events are delivered in). This operation is equivalent to translating the point by the location of the component (which is the origin of its coordinate space).

Parameters: p The point to convert c The component which the point is expressed in terms of

See Also: SwingUtilities

convertRectangle

public static Rectangle convertRectangle(Component source, Rectangle rect, Component destination)
Converts a rectangle from the coordinate space of one component to another. This is equivalent to converting the rectangle from source space to screen space, then back from screen space to destination space. If exactly one of the two Components is null, it is taken to refer to the root ancestor of the other component. If both are null, no transformation is done.

Parameters: source The component which the rectangle is expressed in terms of rect The rectangle to convert destination The component which the return value will be expressed in terms of

Returns: A new rectangle, equal in size to the input rectangle, but with its position converted from the coordinate space of the source component to the coordinate space of the destination component

See Also: SwingUtilities SwingUtilities SwingUtilities SwingUtilities

findFocusOwner

public static Component findFocusOwner(Component comp)

Deprecated: 1.4 Replaced by KeyboardFocusManager.getFocusOwner().

Returns the focus owner or null if comp is not the focus owner or a parent of it.

Parameters: comp the focus owner or a parent of it

Returns: the focus owner, or null

getAccessibleAt

public static Accessible getAccessibleAt(Component c, Point p)
Returns the Accessible child of the specified component which appears at the supplied Point. If there is no child located at that particular pair of co-ordinates, null is returned instead.

Parameters: c the component whose children may be found at the specified point. p the point at which to look for the existence of children of the specified component.

Returns: the Accessible child at the point, p, or null if there is no child at this point.

See Also: AccessibleComponent

getAccessibleChild

public static Accessible getAccessibleChild(Component c, int i)

Returns the Accessible child of the specified component that has the supplied index within the parent component. The indexing of the children is zero-based, making the first child have an index of 0.

Caution is advised when using this method, as its operation relies on the behaviour of varying implementations of an abstract method. For greater surety, direct use of the AWT component implementation of this method is advised.

Parameters: c the component whose child should be returned. i the index of the child within the parent component.

Returns: the Accessible child at index i in the component, c.

See Also: AccessibleContext AccessibleAWTComponent

getAccessibleChildrenCount

public static int getAccessibleChildrenCount(Component c)

Returns the number of Accessible children within the supplied component.

Caution is advised when using this method, as its operation relies on the behaviour of varying implementations of an abstract method. For greater surety, direct use of the AWT component implementation of this method is advised.

Parameters: c the component whose children should be counted.

Returns: the number of children belonging to the component, c.

See Also: AccessibleContext AccessibleAWTComponent

getAccessibleIndexInParent

public static int getAccessibleIndexInParent(Component c)

Returns the zero-based index of the specified component within its parent. If the component doesn't have a parent, -1 is returned.

Caution is advised when using this method, as its operation relies on the behaviour of varying implementations of an abstract method. For greater surety, direct use of the AWT component implementation of this method is advised.

Parameters: c the component whose parental index should be found.

Returns: the index of the component within its parent, or -1 if the component doesn't have a parent.

See Also: AccessibleContext AccessibleAWTComponent

getAccessibleStateSet

public static AccessibleStateSet getAccessibleStateSet(Component c)

Returns a set of AccessibleStates, which represent the state of the supplied component.

Caution is advised when using this method, as its operation relies on the behaviour of varying implementations of an abstract method. For greater surety, direct use of the AWT component implementation of this method is advised.

Parameters: c the component whose accessible state should be retrieved.

Returns: a set of AccessibleState objects, which represent the state of the supplied component.

See Also: AccessibleContext AccessibleAWTComponent

getAncestorNamed

public static Container getAncestorNamed(String name, Component comp)
Returns the least ancestor of comp which has the specified name.

Parameters: name The name to search for comp The component to search the ancestors of

Returns: The nearest ancestor of comp with the given name, or null if no such ancestor exists

See Also: Component SwingUtilities

getAncestorOfClass

public static Container getAncestorOfClass(Class<?> c, Component comp)
Returns the least ancestor of comp which is an instance of the specified class.

Parameters: c The class to search for comp The component to search the ancestors of

Returns: The nearest ancestor of comp which is an instance of the given class, or null if no such ancestor exists

See Also: SwingUtilities SwingUtilities

getDeepestComponentAt

public static Component getDeepestComponentAt(Component parent, int x, int y)
Returns the deepest descendent of parent which is both visible and contains the point (x,y). Returns parent when either parent is not a container, or has no children which contain (x,y). Returns null when either (x,y) is outside the bounds of parent, or parent is null.

Parameters: parent The component to search the descendents of x Horizontal coordinate to search for y Vertical coordinate to search for

Returns: A component containing (x,y), or null

See Also: Container

getLocalBounds

public static Rectangle getLocalBounds(Component aComponent)
Calculates the bounds of a component in the component's own coordinate space. The result has the same height and width as the component's bounds, but its location is set to (0,0).

Parameters: aComponent The component to measure

Returns: The component's bounds in its local coordinate space

getRoot

public static Component getRoot(Component comp)
Returns the "root" of the component tree containint comp The root is defined as either the least ancestor of comp which is a {@link Window}, or the greatest ancestor of comp which is a {@link Applet} if no {@link Window} ancestors are found.

Parameters: comp The component to search for a root

Returns: The root of the component's tree, or null

getRootPane

public static JRootPane getRootPane(Component comp)
If comp is a RootPaneContainer, return its JRootPane. Otherwise call getAncestorOfClass(JRootPane.class, a).

Parameters: comp The component to get the JRootPane of

Returns: a suitable JRootPane for comp, or null

See Also: RootPaneContainer SwingUtilities

getUIActionMap

public static ActionMap getUIActionMap(JComponent component)
Returns the ActionMap that is provided by the ComponentUI of component.

Parameters: component the component for which the ActionMap is returned

getUIInputMap

public static InputMap getUIInputMap(JComponent component, int cond)
Returns the InputMap that is provided by the ComponentUI of component for the specified condition.

Parameters: component the component for which the InputMap is returned cond the condition that specifies which of the three input maps should be returned, may be {@link JComponent#WHEN_IN_FOCUSED_WINDOW}, {@link JComponent#WHEN_FOCUSED} or {@link JComponent#WHEN_ANCESTOR_OF_FOCUSED_COMPONENT}

Returns: The input map.

getWindowAncestor

public static Window getWindowAncestor(Component comp)
Returns the first ancestor of comp that is a {@link Window} or null if comp is not contained in a {@link Window}. This is equivalent to calling getAncestorOfClass(Window, comp) or windowForComponent(comp).

Parameters: comp the component for which we are searching the ancestor Window

Returns: the first ancestor Window of comp or null if comp is not contained in a Window

invokeAndWait

public static void invokeAndWait(Runnable doRun)
Calls {@link java.awt.EventQueue#invokeAndWait} with the specified {@link Runnable}.

invokeLater

public static void invokeLater(Runnable doRun)
Calls {@link java.awt.EventQueue#invokeLater} with the specified {@link Runnable}.

isDescendingFrom

public static boolean isDescendingFrom(Component a, Component b)
Return true if a descends from b, in other words if b is an ancestor of a.

Parameters: a The child to search the ancestry of b The potential ancestor to search for

Returns: true if a is a descendent of b, false otherwise

isEventDispatchThread

public static boolean isEventDispatchThread()
Calls {@link java.awt.EventQueue#isDispatchThread()}.

Returns: true if the current thread is the current AWT event dispatch thread.

isLeftMouseButton

public static boolean isLeftMouseButton(MouseEvent event)
Checks if left mouse button was clicked.

Parameters: event the event to check

Returns: true if left mouse was clicked, false otherwise.

isMiddleMouseButton

public static boolean isMiddleMouseButton(MouseEvent event)
Checks if middle mouse button was clicked.

Parameters: event the event to check

Returns: true if middle mouse was clicked, false otherwise.

isRectangleContainingRectangle

public static boolean isRectangleContainingRectangle(Rectangle a, Rectangle b)
Tests if a rectangle contains another.

Parameters: a first rectangle b second rectangle

Returns: true if a contains b, false otherwise

Throws: NullPointerException

isRightMouseButton

public static boolean isRightMouseButton(MouseEvent event)
Checks if right mouse button was clicked.

Parameters: event the event to check

Returns: true if right mouse was clicked, false otherwise.

layoutCompoundLabel

public static String layoutCompoundLabel(JComponent c, FontMetrics fm, String text, Icon icon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewR, Rectangle iconR, Rectangle textR, int textIconGap)

Layout a "compound label" consisting of a text string and an icon which is to be placed near the rendered text. Once the text and icon are laid out, the text rectangle and icon rectangle parameters are altered to store the calculated positions.

The size of the text is calculated from the provided font metrics object. This object should be the metrics of the font you intend to paint the label with.

The position values control where the text is placed relative to the icon. The horizontal position value should be one of the constants LEADING, TRAILING, LEFT, RIGHT or CENTER. The vertical position value should be one fo the constants TOP, BOTTOM or CENTER.

The text-icon gap value controls the number of pixels between the icon and the text.

The alignment values control where the text and icon are placed, as a combined unit, within the view rectangle. The horizontal alignment value should be one of the constants LEADING, TRAILING, LEFT, RIGHT or CENTER. The vertical alignment valus should be one of the constants TOP, BOTTOM or CENTER.

If the LEADING or TRAILING constants are given for horizontal alignment or horizontal text position, they are interpreted relative to the provided component's orientation property, a constant in the {@link java.awt.ComponentOrientation} class. For example, if the component's orientation is LEFT_TO_RIGHT, then the LEADING value is a synonym for LEFT and the TRAILING value is a synonym for RIGHT

If the text and icon are equal to or larger than the view rectangle, the horizontal and vertical alignment values have no affect.

Parameters: c A component used for its orientation value fm The font metrics used to measure the text text The text to place in the compound label icon The icon to place next to the text verticalAlignment The vertical alignment of the label relative to its component horizontalAlignment The horizontal alignment of the label relative to its component verticalTextPosition The vertical position of the label's text relative to its icon horizontalTextPosition The horizontal position of the label's text relative to its icon viewR The view rectangle, specifying the area which layout is constrained to iconR A rectangle which is modified to hold the laid-out position of the icon textR A rectangle which is modified to hold the laid-out position of the text textIconGap The distance between text and icon

Returns: The string of characters, possibly truncated with an elipsis, which is laid out in this label

layoutCompoundLabel

public static String layoutCompoundLabel(FontMetrics fm, String text, Icon icon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewR, Rectangle iconR, Rectangle textR, int textIconGap)

Layout a "compound label" consisting of a text string and an icon which is to be placed near the rendered text. Once the text and icon are laid out, the text rectangle and icon rectangle parameters are altered to store the calculated positions.

The size of the text is calculated from the provided font metrics object. This object should be the metrics of the font you intend to paint the label with.

The position values control where the text is placed relative to the icon. The horizontal position value should be one of the constants LEFT, RIGHT or CENTER. The vertical position value should be one fo the constants TOP, BOTTOM or CENTER.

The text-icon gap value controls the number of pixels between the icon and the text.

The alignment values control where the text and icon are placed, as a combined unit, within the view rectangle. The horizontal alignment value should be one of the constants LEFT, RIGHT or CENTER. The vertical alignment valus should be one of the constants TOP, BOTTOM or CENTER.

If the text and icon are equal to or larger than the view rectangle, the horizontal and vertical alignment values have no affect.

Note that this method does not know how to deal with horizontal alignments or positions given as LEADING or TRAILING values. Use the other overloaded variant of this method if you wish to use such values.

Parameters: fm The font metrics used to measure the text text The text to place in the compound label icon The icon to place next to the text verticalAlignment The vertical alignment of the label relative to its component horizontalAlignment The horizontal alignment of the label relative to its component verticalTextPosition The vertical position of the label's text relative to its icon horizontalTextPosition The horizontal position of the label's text relative to its icon viewR The view rectangle, specifying the area which layout is constrained to iconR A rectangle which is modified to hold the laid-out position of the icon textR A rectangle which is modified to hold the laid-out position of the text textIconGap The distance between text and icon

Returns: The string of characters, possibly truncated with an elipsis, which is laid out in this label

notifyAction

public static boolean notifyAction(Action action, KeyStroke ks, KeyEvent event, Object sender, int modifiers)

paintComponent

public static void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h)
This method paints the given component at the given position and size. The component will be reparented to the container given.

Parameters: g The Graphics object to draw with. c The Component to draw p The Container to reparent to. x The x coordinate to draw at. y The y coordinate to draw at. w The width of the drawing area. h The height of the drawing area.

paintComponent

public static void paintComponent(Graphics g, Component c, Container p, Rectangle r)
This method paints the given component in the given rectangle. The component will be reparented to the container given.

Parameters: g The Graphics object to draw with. c The Component to draw p The Container to reparent to. r The rectangle that describes the drawing area.

processKeyBindings

public static boolean processKeyBindings(KeyEvent ev)
Processes key bindings for the component that is associated with the key event. Note that this method does not make sense for JComponent-derived components, except when {@link JComponent#processKeyEvent(KeyEvent)} is overridden and super is not called. This method searches through the component hierarchy of the component's top-level container to find a JComponent that has a binding for the key event in the WHEN_IN_FOCUSED_WINDOW scope.

Parameters: ev the key event

Returns: true if a binding has been found and processed, false otherwise

Since: 1.4

replaceUIActionMap

public static void replaceUIActionMap(JComponent component, ActionMap uiActionMap)

Change the shared, UI-managed {@link ActionMap} for a given component. ActionMaps are arranged in a hierarchy, in order to encourage sharing of common actions between components. The hierarchy unfortunately places UI-managed ActionMaps at the end of the parent-pointer chain, as illustrated:

  [{@link javax.swing.JComponent#getActionMap()}] 
          --> [{@link javax.swing.ActionMap}] 
     parent --> [{@link javax.swing.text.JTextComponent.KeymapActionMap}] 
       parent --> [{@link javax.swing.plaf.ActionMapUIResource}]
 

Our goal with this method is to replace the first ActionMap along this chain which is an instance of {@link ActionMapUIResource}, since these are the ActionMaps which are supposed to be shared between components.

If the provided ActionMap is null, we interpret the call as a request to remove the UI-managed ActionMap from the component's ActionMap parent chain.

replaceUIInputMap

public static void replaceUIInputMap(JComponent component, int condition, InputMap uiInputMap)

Change the shared, UI-managed {@link InputMap} for a given component. InputMaps are arranged in a hierarchy, in order to encourage sharing of common input mappings between components. The hierarchy unfortunately places UI-managed InputMaps at the end of the parent-pointer chain, as illustrated:

  [{@link javax.swing.JComponent#getInputMap()}] 
          --> [{@link javax.swing.InputMap}] 
     parent --> [{@link javax.swing.text.JTextComponent.KeymapWrapper}] 
       parent --> [{@link javax.swing.plaf.InputMapUIResource}]
 

Our goal with this method is to replace the first InputMap along this chain which is an instance of {@link InputMapUIResource}, since these are the InputMaps which are supposed to be shared between components.

If the provided InputMap is null, we interpret the call as a request to remove the UI-managed InputMap from the component's InputMap parent chain.

updateComponentTreeUI

public static void updateComponentTreeUI(Component comp)
Recursively walk the component tree under comp calling updateUI on each {@link JComponent} found. This causes the entire tree to re-initialize its UI delegates.

Parameters: comp The component to walk the children of, calling updateUI

windowForComponent

public static Window windowForComponent(Component comp)
Equivalent to calling getAncestorOfClass(Window, comp).

Parameters: comp The component to search for an ancestor window

Returns: An ancestral window, or null if none exists