APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing

public Class SwingUtilities

extends Object
implements SwingConstants
Class Inheritance
All Implemented Interfaces
javax.swing.SwingConstants
Imports
sun.swing.SwingUtilities2, .UIAction, java.awt.*, java.awt.event.*, java.awt.dnd.DropTarget, java.lang.reflect.*, javax.accessibility.*, javax.swing.event.MenuDragMouseEvent, javax.swing.plaf.UIResource, javax.swing.text.View, sun.awt.AWTAccessor, .AWTAccessor.MouseEventAccessor

A collection of utility methods for Swing.
Since
1.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
private static boolean
checkedSuppressDropSupport

Indicates if we've checked the system property for suppressing drop support.

private static Frame
private static boolean
suppressDropSupport

Indicates if we should change the drop target when a TransferHandler is set.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static Rectangle

Returns:

null if the Component is null; otherwise, returns the passed-in rectangle (if non-null) or a new rectangle specifying position and size information
calculateInnerArea
(JComponent
the JComponent in question; if null, this method returns null
c
,
Rectangle
the Rectangle instance to be modified; may be null
r
)

Stores the position and size of the inner painting area of the specified component in r and returns r.

private static boolean

Returns:

true if the anEvent contains specified mouseButton or mouseButtonDownMask based on MouseEvent ID.
checkMouseButton
(MouseEvent
a MouseEvent object
anEvent
,
int
mouse button type
mouseButton
,
int
mouse button down mask event modifier
mouseButtonDownMask
)

Check whether MouseEvent contains specified mouse button or mouse button down mask based on MouseEvent ID.

public static Rectangle[]

Returns:

an array of rectangles representing the regions within rectA that do not overlap with rectB.
computeDifference
(Rectangle
the first rectangle
rectA
,
Rectangle
the second rectangle
rectB
)

Convenience returning an array of rect representing the regions within rectA that do not overlap with rectB.

public static Rectangle

Returns:

dest, modified to specify the intersection
computeIntersection
(int
the X coordinate of the first rectangle's top-left point
x
,
int
the Y coordinate of the first rectangle's top-left point
y
,
int
the width of the first rectangle
width
,
int
the height of the first rectangle
height
,
Rectangle
the second rectangle
dest
)

Convenience to calculate the intersection of two rectangles without allocating a new rectangle.

public static int

Returns:

an int containing the string width
computeStringWidth
(FontMetrics
a FontMetrics object to compute with
fm
,
String
the String to compute
str
)

Compute the width of the string using a font with the specified "metrics" (sizes).

public static Rectangle

Returns:

the dest Rectangle
computeUnion
(int
the x-coordinate of the first rectangle
x
,
int
the y-coordinate of the first rectangle
y
,
int
the width of the first rectangle
width
,
int
the height of the first rectangle
height
,
Rectangle
the coordinates of the second rectangle; the union of the two rectangles is returned in this rectangle
dest
)

Convenience method that calculates the union of two rectangles without allocating a new rectangle.

public static MouseEvent

Returns:

the new mouse event
convertMouseEvent
(Component
the source component
source
,
MouseEvent
the source mouse event
sourceEvent
,
Component
the destination component
destination
)

Returns a MouseEvent similar to sourceEvent except that its x and y members have been converted to destination's coordinate system.

public static Point

Returns:

the converted coordinate
convertPoint
(Component
the source component
source
,
Point
the point
aPoint
,
Component
the destination component
destination
)

Convert a aPoint in source coordinate system to destination coordinate system.

public static Point

Returns:

the converted coordinate
convertPoint
(Component
the source component
source
,
int
the x-coordinate of the point
x
,
int
the y-coordinate of the point
y
,
Component
the destination component
destination
)

Convert the point (x,y) in source coordinate system to destination coordinate system.

public static void
convertPointFromScreen(Point
a Point object (converted to the new coordinate system)
p
,
Component
a Component object
c
)

Convert a point from a screen coordinates to a component's coordinate system

public static void
convertPointToScreen(Point
a Point object (converted to the new coordinate system)
p
,
Component
a Component object
c
)

Convert a point from a component's coordinate system to screen coordinates.

public static Rectangle

Returns:

the converted rectangle
convertRectangle
(Component
the source component
source
,
Rectangle
a rectangle
aRectangle
,
Component
the destination component
destination
)

Convert the rectangle aRectangle in source coordinate system to destination coordinate system.

pack-priv static Point
convertScreenLocationToParent(Container parent, int x, int y)

Converts the location x y to the parents coordinate system, returning the location.

pack-priv static boolean
doesIconReferenceImage(Icon icon, Image image)

Returns true if the Icon icon is an instance of ImageIcon, and the image it contains is the same as image.

pack-priv static int

Returns:

index into the string if exists, otherwise -1
findDisplayedMnemonicIndex
(String
The text to search through, may be null
text
,
int
The mnemonic to find the character for.
mnemonic
)

Returns index of the first occurrence of mnemonic within string text.

public static Component

Returns:

the focus owner, or null if there is no focus owner, or if the focus owner is not comp, or a descendant of comp
findFocusOwner
(Component
the root of the Component hierarchy to search for the focus owner
c
)
Deprecated As of 1.4, replaced by KeyboardFocusManager.getFocusOwner().

Return the child Component of the specified Component that is the focus owner, if any.

public static Accessible

Returns:

the Accessible at the specified location, if it exists; otherwise null
getAccessibleAt
(Component
the component
c
,
Point
the local coordinate
p
)

Returns the Accessible child contained at the local coordinate Point, if one exists.

public static Accessible

Returns:

the nth Accessible child of the object
getAccessibleChild
(Component
the component
c
,
int
zero-based index of child
i
)

Return the nth Accessible child of the object.

public static int

Returns:

the number of accessible children in the object.
getAccessibleChildrenCount
(Component
the component
c
)

Returns the number of accessible children in the object.

public static int

Returns:

-1 of this object does not have an accessible parent. Otherwise, the index of the child in its accessible parent.
getAccessibleIndexInParent
(Component
the component
c
)

Get the index of this object in its accessible parent.

public static AccessibleStateSet

Returns:

an instance of AccessibleStateSet containing the current state set of the object
getAccessibleStateSet
(Component
the component
c
)

Get the state of this object.

public static Container

Returns:

the ancestor of the comp, or null if name cannot be found.
getAncestorNamed
(String
the name of a component
name
,
Component
the component
comp
)

Convenience method for searching above comp in the component hierarchy and returns the first object of name it finds.

public static Container

Returns:

the ancestor of the comp, or null if c cannot be found.
getAncestorOfClass
(Class<?>
the class of a component
c
,
Component
the component
comp
)

Convenience method for searching above comp in the component hierarchy and returns the first object of class c it finds.

private static CellRendererPane
public static Component

Returns:

the deepest component
getDeepestComponentAt
(Component
the root component to begin the search
parent
,
int
the x target location
x
,
int
the y target location
y
)

Returns the deepest visible descendent Component of parent that contains the location x, y.

public static Rectangle

Returns:

the local bounds for the component aComponent
getLocalBounds
(Component
a component
aComponent
)

Return the rectangle (0,0,bounds.width,bounds.height) for the component aComponent

pack-priv static JComponent
public static Component

Returns:

the first ancestor of c that's a Window
getRoot
(Component
the component
c
)

Returns the root component for the current component tree.

public static JRootPane

Returns:

the JRootPane for Component c or null.
getRootPane
(Component
the component
c
)

If c is a JRootPane descendant return its JRootPane ancestor.

pack-priv static Frame
getSharedOwnerFrame()

Returns a toolkit-private, shared, invisible Frame to be the owner for JDialogs and JWindows created with null owners.

pack-priv static WindowListener
getSharedOwnerFrameShutdownListener()

Returns a SharedOwnerFrame's shutdown listener to dispose the SharedOwnerFrame if it has no more displayable children.

private static boolean
getSuppressDropTarget()

Returns true if setTransferHandler should change the DropTarget.

public static ActionMap

Returns:

the ActionMap provided by the UI in the component, or null if the UI has not installed an ActionMap.
getUIActionMap
(JComponent
a component
component
)

Returns the ActionMap provided by the UI in component component.

public static InputMap

Returns:

the ActionMap provided by the UI for condition in the component, or null if the UI has not installed an InputMap of the specified type.
getUIInputMap
(JComponent
a component
component
,
int
a condition
condition
)

Returns the InputMap provided by the UI for condition condition in component component.

public static Container

Returns:

the first ancestor of the component which is not an instance of JLayer. If such an ancestor can not be found, null is returned.
getUnwrappedParent
(Component
Component to get the first ancestor of, which is not a JLayer instance.
component
)

Returns the first ancestor of the component which is not an instance of JLayer.

public static Component

Returns:

the first JViewport's descendant which is not an instance of JLayer. If such a descendant can not be found, null is returned.
getUnwrappedView
(JViewport
JViewport to get the first descendant of, which in not a JLayer instance.
viewport
)

Returns the first JViewport's descendant which is not an instance of JLayer.

pack-priv static Container

Returns:

the validate root of the given container or null
getValidateRoot
(Container c, boolean visibleOnly)

Retrieves the validate root of a given container.

public static Window

Returns:

the first Window ancestor of c, or null if c is not contained inside a Window.
getWindowAncestor
(Component
Component to get Window ancestor of.
c
)

Returns the first Window ancestor of c, or null if c is not contained inside a Window.

pack-priv static void
installSwingDropTargetAsNecessary(Component c, TransferHandler t)

Installs a DropTarget on the component as necessary for a TransferHandler change.

public static void
invokeAndWait(final Runnable
the instance of Runnable
doRun
)

Causes doRun.run() to be executed synchronously on the AWT event dispatching thread.

public static void
invokeLater(Runnable
the instance of Runnable
doRun
)

Causes doRun.run() to be executed asynchronously on the AWT event dispatching thread.

public static boolean

Returns:

true if a component a descends from a component b
isDescendingFrom
(Component
the first component
a
,
Component
the second component
b
)

Return true if a component a descends from a component b

public static boolean

Returns:

true if the current thread is an AWT event dispatching thread
isEventDispatchThread
()

Returns true if the current thread is an AWT event dispatching thread.

public static boolean

Returns:

true if the left mouse button was active
isLeftMouseButton
(MouseEvent
a MouseEvent object
anEvent
)

Returns true if the mouse event specifies the left mouse button.

pack-priv static boolean
public static boolean

Returns:

true if the middle mouse button was active
isMiddleMouseButton
(MouseEvent
a MouseEvent object
anEvent
)

Returns true if the mouse event specifies the middle mouse button.

public static final boolean

Returns:

true if a contains b
isRectangleContainingRectangle
(Rectangle
the first rectangle
a
,
Rectangle
the second rectangle
b
)

Return true if a contains b

public static boolean

Returns:

true if the right mouse button was active
isRightMouseButton
(MouseEvent
a MouseEvent object
anEvent
)

Returns true if the mouse event specifies the right mouse button.

pack-priv static boolean
isValidKeyEventForKeyBindings(KeyEvent e)

Returns true if the e is a valid KeyEvent to use in processing the key bindings associated with JComponents.

public static String

Returns:

the possibly clipped version of the compound labels string
layoutCompoundLabel
(JComponent
the component
c
,
FontMetrics
the instance of FontMetrics
fm
,
String
the text
text
,
Icon
the icon
icon
,
int
the vertical alignment
verticalAlignment
,
int
the horizontal alignment
horizontalAlignment
,
int
the vertical text position
verticalTextPosition
,
int
the horizontal text position
horizontalTextPosition
,
Rectangle
the available rectangle
viewR
,
Rectangle
the rectangle for the icon
iconR
,
Rectangle
the rectangle for the text
textR
,
int
the gap between text and icon
textIconGap
)

Compute and return the location of the icons origin, the location of origin of the text baseline, and a possibly clipped version of the compound labels string.

public static String

Returns:

the possibly clipped version of the compound labels string
layoutCompoundLabel
(FontMetrics
the instance of FontMetrics
fm
,
String
the text
text
,
Icon
the icon
icon
,
int
the vertical alignment
verticalAlignment
,
int
the horizontal alignment
horizontalAlignment
,
int
the vertical text position
verticalTextPosition
,
int
the horizontal text position
horizontalTextPosition
,
Rectangle
the available rectangle
viewR
,
Rectangle
the rectangle for the icon
iconR
,
Rectangle
the rectangle for the text
textR
,
int
the gap between text and icon
textIconGap
)

Compute and return the location of the icons origin, the location of origin of the text baseline, and a possibly clipped version of the compound labels string.

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

Compute and return the location of the icons origin, the location of origin of the text baseline, and a possibly clipped version of the compound labels string.

pack-priv static Class<?>
public static boolean

Returns:

true if action is non-null and actionPerformed is invoked on it.
notifyAction
(Action
an action
action
,
KeyStroke
a key stroke
ks
,
KeyEvent
a key event
event
,
Object
a sender
sender
,
int
action modifiers
modifiers
)

Invokes actionPerformed on action if action is non-null and accepts the sender object.

public static void
paintComponent(Graphics
the Graphics object to draw on
g
,
Component
the Component to draw
c
,
Container
the intermediate Container
p
,
int
an int specifying the left side of the area draw in, in pixels, measured from the left edge of the graphics context
x
,
int
an int specifying the top of the area to draw in, in pixels measured down from the top edge of the graphics context
y
,
int
an int specifying the width of the area draw in, in pixels
w
,
int
an int specifying the height of the area draw in, in pixels
h
)

Paints a component to the specified Graphics.

public static void
paintComponent(Graphics
the Graphics object to draw on
g
,
Component
the Component to draw
c
,
Container
the intermediate Container
p
,
Rectangle
the Rectangle to draw in
r
)

Paints a component to the specified Graphics.

public static boolean

Returns:

true if a binding has found and processed
processKeyBindings
(KeyEvent
KeyEvent used to identify which bindings to process, as well as which Component has focus.
event
)

Process the key bindings for the Component associated with event.

public static void
replaceUIActionMap(JComponent
a component
component
,
ActionMap
an ActionMap
uiActionMap
)

Convenience method to change the UI ActionMap for component to uiActionMap.

public static void
replaceUIInputMap(JComponent
a component
component
,
int
a type
type
,
InputMap
an InputMap
uiInputMap
)

Convenience method to change the UI InputMap for component to uiInputMap.

public static void
updateComponentTreeUI(Component
the component
c
)

A simple minded look and feel change: ask each node in the tree to updateUI() -- that is, to initialize its UI property with the current look and feel.

private static void
pack-priv static void
updateRendererOrEditorUI(Object rendererOrEditor)

public static Window

Returns:

the first Window ancestor of c, or null if c is not contained inside a Window.
windowForComponent
(Component
Component to get Window ancestor of.
c
)

Returns the first Window ancestor of c, or null if c is not contained inside a Window.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait