java.lang.Objectjavax.swing.SwingUtilities
public class SwingUtilities
A collection of utility methods for Swing.
Field Summary |
---|
Fields inherited from interface javax.swing. SwingConstants |
---|
BOTTOM , CENTER , EAST , HORIZONTAL , LEADING , LEFT , NEXT , NORTH , NORTH_EAST , NORTH_WEST , PREVIOUS , RIGHT , SOUTH , SOUTH_EAST , SOUTH_WEST , TOP , TRAILING , VERTICAL , WEST |
Method Summary | |
---|---|
static Rectangle |
calculateInnerArea
(
JComponent
c,
Rectangle
r) Stores the position and size of the inner painting area of the specified component in r and returns r. |
static Rectangle [] |
computeDifference
(
Rectangle
rectA,
Rectangle
rectB) Convenience returning an array of rect representing the regions within rectA that do not overlap with rectB. |
static Rectangle |
computeIntersection
(int x, int y, int width, int height,
Rectangle
dest) Convenience to calculate the intersection of two rectangles without allocating a new rectangle. |
static int |
computeStringWidth
(
FontMetrics
fm,
String
str) Compute the width of the string using a font with the specified "metrics" (sizes). |
static Rectangle |
computeUnion
(int x, int y, int width, int height,
Rectangle
dest) Convenience method that calculates the union of two rectangles without allocating a new rectangle. |
static MouseEvent |
convertMouseEvent
(
Component
source,
MouseEvent
sourceEvent,
Component
destination) Returns a MouseEvent similar to sourceEvent except that its x and y members have been converted to destination's coordinate system. |
static Point |
convertPoint
(
Component
source, int x, int y,
Component
destination) Convert the point (x,y) in source coordinate system to destination coordinate system. |
static Point |
convertPoint
(
Component
source,
Point
aPoint,
Component
destination) Convert a aPoint in source coordinate system to destination coordinate system. |
static void |
convertPointFromScreen
(
Point
p,
Component
c) Convert a point from a screen coordinates to a component's coordinate system |
static void |
convertPointToScreen
(
Point
p,
Component
c) Convert a point from a component's coordinate system to screen coordinates. |
static Rectangle |
convertRectangle
(
Component
source,
Rectangle
aRectangle,
Component
destination) Convert the rectangle aRectangle in source coordinate system to destination coordinate system. |
static Component |
findFocusOwner
(
Component
c) Deprecated. As of 1.4, replaced by KeyboardFocusManager.getFocusOwner(). |
static Accessible |
getAccessibleAt
(
Component
c,
Point
p) Returns the Accessible child contained at the local coordinate Point, if one exists. |
static Accessible |
getAccessibleChild
(
Component
c, int i) Return the nth Accessible child of the object. |
static int |
getAccessibleChildrenCount
(
Component
c) Returns the number of accessible children in the object. |
static int |
getAccessibleIndexInParent
(
Component
c) Get the index of this object in its accessible parent. |
static AccessibleStateSet |
getAccessibleStateSet
(
Component
c) Get the state of this object. |
static Container |
getAncestorNamed
(
String
name,
Component
comp) Convenience method for searching above comp in the component hierarchy and returns the first object of name it finds. |
static Container |
getAncestorOfClass
(
Class
<?> c,
Component
comp) Convenience method for searching above comp in the component hierarchy and returns the first object of class c it finds. |
static Component |
getDeepestComponentAt
(
Component
parent, int x, int y) Returns the deepest visible descendent Component of parent that contains the location x, y. |
static Rectangle |
getLocalBounds
(
Component
aComponent) Return the rectangle (0,0,bounds.width,bounds.height) for the component aComponent |
static Component |
getRoot
(
Component
c) Returns the root component for the current component tree. |
static JRootPane |
getRootPane
(
Component
c) If c is a JRootPane descendant return its JRootPane ancestor. |
static ActionMap |
getUIActionMap
(
JComponent
component) Returns the ActionMap provided by the UI in component component. |
static InputMap |
getUIInputMap
(
JComponent
component, int condition) Returns the InputMap provided by the UI for condition condition in component component. |
static Window |
getWindowAncestor
(
Component
c) Returns the first Window ancestor of c, or null if c is not contained inside a Window. |
static void |
invokeAndWait
(
Runnable
doRun) Causes doRun.run() to be executed synchronously on the AWT event dispatching thread. |
static void |
invokeLater
(
Runnable
doRun) Causes doRun.run() to be executed asynchronously on the AWT event dispatching thread. |
static boolean |
isDescendingFrom
(
Component
a,
Component
b) Return true if a component a descends from a component b |
static boolean |
isEventDispatchThread
() Returns true if the current thread is an AWT event dispatching thread. |
static boolean |
isLeftMouseButton
(
MouseEvent
anEvent) Returns true if the mouse event specifies the left mouse button. |
static boolean |
isMiddleMouseButton
(
MouseEvent
anEvent) Returns true if the mouse event specifies the middle mouse button. |
static boolean |
isRectangleContainingRectangle
(
Rectangle
a,
Rectangle
b) Return true if a contains b |
static boolean |
isRightMouseButton
(
MouseEvent
anEvent) Returns true if the mouse event specifies the right mouse button. |
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) 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. |
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) 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. |
static boolean |
notifyAction
(
Action
action,
KeyStroke
ks,
KeyEvent
event,
Object
sender, int modifiers) Invokes actionPerformed on action if action is enabled (and non null). |
static void |
paintComponent
(
Graphics
g,
Component
c,
Container
Paints a component ![]() ![]() ![]() ![]() |
static void |
paintComponent
(
Graphics
g,
Component
c,
Container
p,
Rectangle
Paints a component ![]() ![]() ![]() ![]() |
static boolean |
processKeyBindings
(
KeyEvent
event) Process the key bindings for the Component associated with event. |
static void |
replaceUIActionMap
(
JComponent
component,
ActionMap
uiActionMap) Convenience method to change the UI ActionMap for component to uiActionMap. |
static void |
replaceUIInputMap
(
JComponent
component, int type,
InputMap
uiInputMap) Convenience method to change the UI InputMap for component to uiInputMap. |
static void |
updateComponentTreeUI
(
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. |
static Window |
windowForComponent
(
Component
c) Returns the first Window ancestor of c, or null if c is not contained inside a Window. |
Methods inherited from class java.lang. Object |
---|
clone , equals , finalize , getClass , hashCode , notify , notifyAll , toString , wait , wait , wait |
Method Detail |
---|
public static final boolean isRectangleContainingRectangle(Rectangle a, Rectangle b)
public static Rectangle getLocalBounds(Component aComponent)
public static Window getWindowAncestor(Component c)
public static Point convertPoint(Component source, Point aPoint, Component destination)
public static Point convertPoint(Component source, int x, int y, Component destination)
public static Rectangle convertRectangle(Component source, Rectangle aRectangle, Component destination)
public static Container getAncestorOfClass(Class<?> c, Component comp)
public static Container getAncestorNamed(String name, Component comp)
public static Component getDeepestComponentAt(Component parent, int x, int y)
public static MouseEvent convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination)
public static void convertPointToScreen(Point p, Component c)
public static void convertPointFromScreen(Point p, Component c)
public static Window windowForComponent(Component c)
Note: This method provides the same functionality as getWindowAncestor.
public static boolean isDescendingFrom(Component a, Component b)
public static Rectangle computeIntersection(int x, int y, int width, int height, Rectangle dest)
public static Rectangle computeUnion(int x, int y, int width, int height, Rectangle dest)
public static Rectangle[] computeDifference(Rectangle rectA, Rectangle rectB)
public static boolean isLeftMouseButton(MouseEvent anEvent)
public static boolean isMiddleMouseButton(MouseEvent anEvent)
public static boolean isRightMouseButton(MouseEvent anEvent)
public static int computeStringWidth(FontMetrics fm, String str)
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)
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)
public static void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h)
This method makes use of CellRendererPane to handle the actual painting, and is only recommended if you use one component for rendering. If you make use of multiple components to handle the rendering, as JTable does, use CellRendererPane directly. Otherwise, as described below, you could end up with a CellRendererPane per Component.
If c's parent is not a CellRendererPane, a new CellRendererPane is created, c is added to it, and the CellRendererPane is added to p. If c's parent is a CellRendererPane and the CellRendererPanes parent is not p, it is added to p.
The component should either descend from JComponent or be another kind of lightweight component. A lightweight component is one whose "lightweight" property (returned by the Component isLightweight method) is true. If the Component is not lightweight, bad things map happen: crashes, exceptions, painting problems...
public static void paintComponent(Graphics g, Component c, Container p, Rectangle r)
The component should either descend from JComponent or be another kind of lightweight component. A lightweight component is one whose "lightweight" property (returned by the Component isLightweight method) is true. If the Component is not lightweight, bad things map happen: crashes, exceptions, painting problems...
public static void updateComponentTreeUI(Component c)
public static void invokeLater(Runnable doRun)
Runnable doHelloWorld = new Runnable() { public void run() { System.out.println("Hello World on " + Thread.currentThread()); } }; SwingUtilities.invokeLater(doHelloWorld); System.out.println("This might well be displayed before the other message.");If invokeLater is called from the event dispatching thread -- for example, from a JButton's ActionListener -- the doRun.run() will still be deferred until all pending events have been processed. Note that if the doRun.run() throws an uncaught exception the event dispatching thread will unwind (not the current thread).
Additional documentation and examples for this method can be found in How to Use Threads , in The Java Tutorial.
As of 1.3 this method is just a cover for java.awt.EventQueue.invokeLater().
public static void invokeAndWait(Runnable doRun) throws InterruptedException, InvocationTargetException
final Runnable doHelloWorld = new Runnable() { public void run() { System.out.println("Hello World on " + Thread.currentThread()); } }; Thread appThread = new Thread() { public void run() { try { SwingUtilities.invokeAndWait(doHelloWorld); } catch (Exception e) { e.printStackTrace(); } System.out.println("Finished on " + Thread.currentThread()); } }; appThread.start();Note that if the Runnable.run method throws an uncaught exception (on the event dispatching thread) it's caught and rethrown, as an InvocationTargetException, on the caller's thread.
Additional documentation and examples for this method can be found in How to Use Threads , in The Java Tutorial.
As of 1.3 this method is just a cover for java.awt.EventQueue.invokeAndWait().
public static boolean isEventDispatchThread()
As of 1.3 this method is just a cover for java.awt.EventQueue.isDispatchThread().
public static int getAccessibleIndexInParent(Component c)
Note: as of the Java 2 platform v1.3, it is recommended that developers call Component.AccessibleAWTComponent.getAccessibleIndexInParent() instead of using this method.
public static Accessible getAccessibleAt(Component c, Point p)
public static AccessibleStateSet getAccessibleStateSet(Component c)
Note: as of the Java 2 platform v1.3, it is recommended that developers call Component.AccessibleAWTComponent.getAccessibleIndexInParent() instead of using this method.
public static int getAccessibleChildrenCount(Component c)
Note: as of the Java 2 platform v1.3, it is recommended that developers call Component.AccessibleAWTComponent.getAccessibleIndexInParent() instead of using this method.
public static Accessible getAccessibleChild(Component c, int i)
Note: as of the Java 2 platform v1.3, it is recommended that developers call Component.AccessibleAWTComponent.getAccessibleIndexInParent() instead of using this method.
@Deprecated public static Component findFocusOwner(Component c)
public static JRootPane getRootPane(Component c)
public static Component getRoot(Component c)
public static boolean processKeyBindings(KeyEvent event)
public static boolean notifyAction(Action action, KeyStroke ks, KeyEvent event, Object sender, int modifiers)
public static void replaceUIInputMap(JComponent component, int type, InputMap uiInputMap)
public static void replaceUIActionMap(JComponent component, ActionMap uiActionMap)
public static InputMap getUIInputMap(JComponent component, int condition)
This will return null if the UI has not installed a InputMap of the specified type.
public static ActionMap getUIActionMap(JComponent component)
This will return null if the UI has not installed an ActionMap.
public static Rectangle calculateInnerArea(JComponent c, Rectangle r)