public class SwingUtilities extends Object implements SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
Modifier and Type | Method and Description |
---|---|
static Rectangle |
calculateInnerArea(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 Rectangle |
computeIntersection(int x,
int y,
int w,
int h,
Rectangle rect)
Calculates the intersection of two rectangles.
|
static int |
computeStringWidth(FontMetrics fm,
String str)
Calculates the width of a given string.
|
static Rectangle |
computeUnion(int x,
int y,
int w,
int h,
Rectangle rect)
Calculates the union of two rectangles.
|
static MouseEvent |
convertMouseEvent(Component source,
MouseEvent sourceEvent,
Component destination)
Convert a mouse event which refrers to one component to another.
|
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. |
static Point |
convertPoint(Component source,
Point aPoint,
Component destination) |
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.
|
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).
|
static Rectangle |
convertRectangle(Component source,
Rectangle rect,
Component destination)
Converts a rectangle from the coordinate space of one component to
another.
|
static Component |
findFocusOwner(Component comp)
Deprecated.
1.4 Replaced by
KeyboardFocusManager.getFocusOwner() . |
static Accessible |
getAccessibleAt(Component c,
Point p)
Returns the
Accessible child of the specified component
which appears at the supplied Point . |
static Accessible |
getAccessibleChild(Component c,
int i)
Returns the
Accessible child of the specified component
that has the supplied index within the parent component. |
static int |
getAccessibleChildrenCount(Component c)
Returns the number of
Accessible children within
the supplied component. |
static int |
getAccessibleIndexInParent(Component c)
Returns the zero-based index of the specified component
within its parent.
|
static AccessibleStateSet |
getAccessibleStateSet(Component c)
Returns a set of
AccessibleState s, which represent
the state of the supplied component. |
static Container |
getAncestorNamed(String name,
Component comp)
Returns the least ancestor of
comp which has the
specified name. |
static Container |
getAncestorOfClass(Class<?> c,
Component comp)
Returns the least ancestor of
comp which is an instance
of the specified class. |
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) . |
static Rectangle |
getLocalBounds(Component aComponent)
Calculates the bounds of a component in the component's own coordinate
space.
|
static Component |
getRoot(Component comp)
|
static JRootPane |
getRootPane(Component comp)
If
comp is a RootPaneContainer, return its JRootPane. |
static ActionMap |
getUIActionMap(JComponent component)
Returns the ActionMap that is provided by the ComponentUI of
component . |
static InputMap |
getUIInputMap(JComponent component,
int cond)
Returns the InputMap that is provided by the ComponentUI of
component for the specified condition. |
static Window |
getWindowAncestor(Component comp)
|
static void |
invokeAndWait(Runnable doRun)
Calls
EventQueue.invokeAndWait(java.lang.Runnable) with the
specified Runnable . |
static void |
invokeLater(Runnable doRun)
Calls
EventQueue.invokeLater(java.lang.Runnable) with the
specified Runnable . |
static boolean |
isDescendingFrom(Component a,
Component b)
Return true if a descends from b, in other words if b is an ancestor of a.
|
static boolean |
isEventDispatchThread()
|
static boolean |
isLeftMouseButton(MouseEvent event)
Checks if left mouse button was clicked.
|
static boolean |
isMiddleMouseButton(MouseEvent event)
Checks if middle mouse button was clicked.
|
static boolean |
isRectangleContainingRectangle(Rectangle a,
Rectangle b)
Tests if a rectangle contains another.
|
static boolean |
isRightMouseButton(MouseEvent event)
Checks if right mouse button was clicked.
|
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.
|
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.
|
static boolean |
notifyAction(Action action,
KeyStroke ks,
KeyEvent event,
Object sender,
int modifiers) |
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.
|
static void |
paintComponent(Graphics g,
Component c,
Container p,
Rectangle r)
This method paints the given component in the given rectangle.
|
static boolean |
processKeyBindings(KeyEvent ev)
Processes key bindings for the component that is associated with the
key event.
|
static void |
replaceUIActionMap(JComponent component,
ActionMap uiActionMap)
Change the shared, UI-managed
ActionMap for a given
component. |
static void |
replaceUIInputMap(JComponent component,
int condition,
InputMap uiInputMap)
Change the shared, UI-managed
InputMap for a given
component. |
static void |
updateComponentTreeUI(Component comp)
|
static Window |
windowForComponent(Component comp)
Equivalent to calling
getAncestorOfClass(Window, comp) . |
public static Rectangle calculateInnerArea(JComponent c, Rectangle r)
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).public static Component findFocusOwner(Component comp)
KeyboardFocusManager.getFocusOwner()
.null
if comp
is not
the focus owner or a parent of it.comp
- the focus owner or a parent of itnull
public static Accessible getAccessibleAt(Component c, Point p)
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.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.Accessible
child at the point, p
,
or null if there is no child at this point.AccessibleComponent.getAccessibleAt(java.awt.Point)
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.
c
- the component whose child should be returned.i
- the index of the child within the parent component.Accessible
child at index i
in the component, c
.AccessibleContext.getAccessibleChild(int)
,
Component.AccessibleAWTComponent.getAccessibleChild(int)
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.
c
- the component whose children should be counted.c
.AccessibleContext.getAccessibleChildrenCount()
,
Component.AccessibleAWTComponent.getAccessibleChildrenCount()
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.
c
- the component whose parental index should be found.AccessibleContext.getAccessibleIndexInParent()
,
Component.AccessibleAWTComponent.getAccessibleIndexInParent()
public static AccessibleStateSet getAccessibleStateSet(Component c)
Returns a set of AccessibleState
s, 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.
c
- the component whose accessible state should be retrieved.AccessibleState
objects, which represent
the state of the supplied component.AccessibleContext.getAccessibleStateSet()
,
Component.AccessibleAWTComponent.getAccessibleStateSet()
public static Rectangle getLocalBounds(Component aComponent)
aComponent
- The component to measurepublic static JRootPane getRootPane(Component comp)
comp
is a RootPaneContainer, return its JRootPane.
Otherwise call getAncestorOfClass(JRootPane.class, a)
.comp
- The component to get the JRootPane ofcomp
, or null
RootPaneContainer.getRootPane()
,
getAncestorOfClass(java.lang.Class<?>, java.awt.Component)
public static Container getAncestorNamed(String name, Component comp)
comp
which has the
specified name.name
- The name to search forcomp
- The component to search the ancestors ofcomp
with the given
name, or null
if no such ancestor existsComponent.getName()
,
getAncestorOfClass(java.lang.Class<?>, java.awt.Component)
public static Container getAncestorOfClass(Class<?> c, Component comp)
comp
which is an instance
of the specified class.c
- The class to search forcomp
- The component to search the ancestors ofcomp
which is an instance
of the given class, or null
if no such ancestor existsgetAncestorOfClass(java.lang.Class<?>, java.awt.Component)
,
windowForComponent(java.awt.Component)
public static Window getWindowAncestor(Component comp)
comp
that is a Window
or null
if comp
is not contained in a
Window
.
This is equivalent to calling
getAncestorOfClass(Window, comp)
or
windowForComponent(comp)
.comp
- the component for which we are searching the ancestor Windowcomp
or
null
if comp
is not contained in a Windowpublic static Window windowForComponent(Component comp)
getAncestorOfClass(Window, comp)
.comp
- The component to search for an ancestor windownull
if none existspublic static Component getRoot(Component comp)
comp
The root is defined as either the least ancestor of
comp
which is a Window
, or the greatest
ancestor of comp
which is a Applet
if no Window
ancestors are found.comp
- The component to search for a rootnull
public static boolean isDescendingFrom(Component a, Component b)
a
- The child to search the ancestry ofb
- The potential ancestor to search forpublic static Component getDeepestComponentAt(Component parent, int x, int y)
(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
.parent
- The component to search the descendents ofx
- Horizontal coordinate to search fory
- Vertical coordinate to search for(x,y)
, or
null
Container.findComponentAt(int, int)
public static void convertPointToScreen(Point p, Component c)
p
- The point to convertc
- The component which the point is expressed in terms ofconvertPointFromScreen(java.awt.Point, java.awt.Component)
public static void convertPointFromScreen(Point p, Component c)
p
- The point to convertc
- The component which the point should be expressed in terms ofpublic static Point convertPoint(Component source, int x, int y, Component destination)
(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.source
- The component which the point is expressed in terms ofx
- Horizontal coordinate of point to transformy
- Vertical coordinate of point to transformdestination
- The component which the return value will be
expressed in terms of(x,y)
converted from the coordinate space of the
source component to the coordinate space of the destination componentconvertPointToScreen(java.awt.Point, java.awt.Component)
,
convertPointFromScreen(java.awt.Point, java.awt.Component)
,
convertRectangle(java.awt.Component, java.awt.Rectangle, java.awt.Component)
,
getRoot(java.awt.Component)
public static Point convertPoint(Component source, Point aPoint, Component destination)
public static Rectangle convertRectangle(Component source, Rectangle rect, Component destination)
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.source
- The component which the rectangle is expressed in terms ofrect
- The rectangle to convertdestination
- The component which the return value will be
expressed in terms ofconvertPointToScreen(java.awt.Point, java.awt.Component)
,
convertPointFromScreen(java.awt.Point, java.awt.Component)
,
convertPoint(Component, int, int, Component)
,
getRoot(java.awt.Component)
public static MouseEvent convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination)
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.source
- The component the mouse event currently refers tosourceEvent
- The mouse event to convertdestination
- The component the new mouse event should refer toconvertPoint(Component, int, int, Component)
public static void updateComponentTreeUI(Component comp)
comp
calling
updateUI
on each JComponent
found. This causes
the entire tree to re-initialize its UI delegates.comp
- The component to walk the children of, calling updateUI
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 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.
c
- A component used for its orientation valuefm
- The font metrics used to measure the texttext
- The text to place in the compound labelicon
- The icon to place next to the textverticalAlignment
- The vertical alignment of the label relative
to its componenthorizontalAlignment
- The horizontal alignment of the label
relative to its componentverticalTextPosition
- The vertical position of the label's text
relative to its iconhorizontalTextPosition
- The horizontal position of the label's
text relative to its iconviewR
- The view rectangle, specifying the area which layout is
constrained toiconR
- A rectangle which is modified to hold the laid-out
position of the icontextR
- A rectangle which is modified to hold the laid-out
position of the texttextIconGap
- The distance between text and iconpublic 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.
fm
- The font metrics used to measure the texttext
- The text to place in the compound labelicon
- The icon to place next to the textverticalAlignment
- The vertical alignment of the label relative
to its componenthorizontalAlignment
- The horizontal alignment of the label
relative to its componentverticalTextPosition
- The vertical position of the label's text
relative to its iconhorizontalTextPosition
- The horizontal position of the label's
text relative to its iconviewR
- The view rectangle, specifying the area which layout is
constrained toiconR
- A rectangle which is modified to hold the laid-out
position of the icontextR
- A rectangle which is modified to hold the laid-out
position of the texttextIconGap
- The distance between text and iconpublic static void invokeLater(Runnable doRun)
EventQueue.invokeLater(java.lang.Runnable)
with the
specified Runnable
.public static void invokeAndWait(Runnable doRun) throws InterruptedException, InvocationTargetException
EventQueue.invokeAndWait(java.lang.Runnable)
with the
specified Runnable
.public static boolean isEventDispatchThread()
true
if the current thread is the current AWT event
dispatch thread.public static void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h)
g
- The Graphics object to draw with.c
- The Component to drawp
- 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.public static void paintComponent(Graphics g, Component c, Container p, Rectangle r)
g
- The Graphics object to draw with.c
- The Component to drawp
- The Container to reparent to.r
- The rectangle that describes the drawing area.public static boolean isLeftMouseButton(MouseEvent event)
event
- the event to checkpublic static boolean isMiddleMouseButton(MouseEvent event)
event
- the event to checkpublic static boolean isRightMouseButton(MouseEvent event)
event
- the event to checkpublic static boolean notifyAction(Action action, KeyStroke ks, KeyEvent event, Object sender, int modifiers)
public static void replaceUIActionMap(JComponent component, ActionMap uiActionMap)
Change the shared, UI-managed 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:
[JComponent.getActionMap()
] --> [ActionMap
] parent --> [JTextComponent.KeymapActionMap
] parent --> [ActionMapUIResource
]
Our goal with this method is to replace the first ActionMap along
this chain which is an instance of 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.
public static void replaceUIInputMap(JComponent component, int condition, InputMap uiInputMap)
Change the shared, UI-managed 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:
[JComponent.getInputMap()
] --> [InputMap
] parent --> [JTextComponent.KeymapWrapper
] parent --> [InputMapUIResource
]
Our goal with this method is to replace the first InputMap along
this chain which is an instance of 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.
public static Rectangle[] computeDifference(Rectangle rectA, Rectangle rectB)
null
, a zero-size array is returned.rectA
- The first rectanglerectB
- The rectangle to subtract from the firstpublic static Rectangle computeIntersection(int x, int y, int w, int h, Rectangle rect)
rect
. This is basically the same
like Rectangle.intersection(Rectangle)
, only that it does not
create new Rectangle instances. The tradeoff is that you loose any data in
rect
.x
- upper-left x coodinate of first rectangley
- upper-left y coodinate of first rectanglew
- width of first rectangleh
- height of first rectanglerect
- a Rectangle object of the second rectangleNullPointerException
- if rect is nullpublic static int computeStringWidth(FontMetrics fm, String str)
fm
- the FontMetrics
object to usestr
- the stringpublic static Rectangle computeUnion(int x, int y, int w, int h, Rectangle rect)
rect
. This is basically the same as
Rectangle.union(Rectangle)
except that it avoids creation of new
Rectangle objects. The tradeoff is that you loose any data in
rect
.x
- upper-left x coodinate of first rectangley
- upper-left y coodinate of first rectanglew
- width of first rectangleh
- height of first rectanglerect
- a Rectangle object of the second rectangleNullPointerException
- if rect is nullpublic static boolean isRectangleContainingRectangle(Rectangle a, Rectangle b)
a
- first rectangleb
- second rectangleNullPointerException
public static InputMap getUIInputMap(JComponent component, int cond)
component
for the specified condition.component
- the component for which the InputMap is returnedcond
- the condition that specifies which of the three input
maps should be returned, may be
JComponent.WHEN_IN_FOCUSED_WINDOW
,
JComponent.WHEN_FOCUSED
or
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
public static ActionMap getUIActionMap(JComponent component)
component
.component
- the component for which the ActionMap is returnedpublic static boolean processKeyBindings(KeyEvent ev)
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.ev
- the key eventtrue
if a binding has been found and processed,
false
otherwise