Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| Swing 1.1
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## javax.swing
Class SwingUtilities

    java.lang.Object
      |
      +--javax.swing.SwingUtilities

public class SwingUtilities
extends java.lang.Object
implements SwingConstants

A collection of utility methods for Swing.

Method Summary
`static java.awt.Rectangle[]` | `computeDifference(java.awt.Rectangle rectA, java.awt.Rectangle rectB)` Convenience returning an array of rect representing the regions within `rectA` that do not overlap with `rectB`.
`static java.awt.Rectangle` | `computeIntersection(int x, int y, int width, int height, java.awt.Rectangle dest)` Convenience to calculate an intersection of two rectangles without allocating a new rectangle Return dest.
`static int` | `computeStringWidth(java.awt.FontMetrics fm, java.lang.String str)` Compute the width of the string using a font with the specified "metrics" (sizes).
`static java.awt.Rectangle` | `computeUnion(int x, int y, int width, int height, java.awt.Rectangle dest)` Convenience to calculate the union of two rectangles without allocating a new rectangle Return dest
`static java.awt.event.MouseEvent` | `convertMouseEvent(java.awt.Component source, java.awt.event.MouseEvent sourceEvent, java.awt.Component destination)` Returns a MouseEvent similar to `sourceEvent` except that its x and y members have been converted to `destination`'s coordinate system.
`static java.awt.Point` | `convertPoint(java.awt.Component source, int x, int y, java.awt.Component destination)` Convert the point `(x,y)` in `source` coordinate system to `destination` coordinate system.
`static java.awt.Point` | `convertPoint(java.awt.Component source, java.awt.Point aPoint, java.awt.Component destination)` Convert a `aPoint` in `source` coordinate system to `destination` coordinate system.
`static void` | `convertPointFromScreen(java.awt.Point p, java.awt.Component c)` Convert a point from a screen coordinates to a component's coordinate system
`static void` | `convertPointToScreen(java.awt.Point p, java.awt.Component c)` Convert a point from a component's coordinate system to screen coordinates.
`static java.awt.Rectangle` | `convertRectangle(java.awt.Component source, java.awt.Rectangle aRectangle, java.awt.Component destination)` Convert the rectangle `aRectangle` in `source` coordinate system to `destination` coordinate system.
`static java.awt.Component` | `findFocusOwner(java.awt.Component c)` Return the child component which has focus, if any.
`static Accessible` | `getAccessibleAt(java.awt.Component c, java.awt.Point p)` Returns the Accessible child contained at the local coordinate Point, if one exists.
`static Accessible` | `getAccessibleChild(java.awt.Component c, int i)` Return the nth Accessible child of the object.
`static int` | `getAccessibleChildrenCount(java.awt.Component c)` Returns the number of accessible children in the object.
`static int` | `getAccessibleIndexInParent(java.awt.Component c)` Get the index of this object in its accessible parent.
`static AccessibleStateSet` | `getAccessibleStateSet(java.awt.Component c)` Get the state of this object.
`static java.awt.Container` | `getAncestorNamed(java.lang.String name, java.awt.Component comp)` Convenience method for searching above `comp` in the component hierarchy and returns the first object of `name` it finds.
`static java.awt.Container` | `getAncestorOfClass(java.lang.Class c, java.awt.Component comp)` Convenience method for searching above `comp` in the component hierarchy and returns the first object of class `c` it finds.
`static java.awt.Component` | `getDeepestComponentAt(java.awt.Component parent, int x, int y)` Returns the deepest visible descendent Component of `parent` that contains the location `x`, `y`.
`static java.awt.Rectangle` | `getLocalBounds(java.awt.Component aComponent)` Return the rectangle (0,0,bounds.width,bounds.height) for the component `aComponent`
`static java.awt.Component` | `getRoot(java.awt.Component c)` Returns the root component for the current component tree.
`static JRootPane` | `getRootPane(java.awt.Component c)` If c is a JRootPane descendant return its JRootPane ancestor.
`static void` | `invokeAndWait(java.lang.Runnable doRun)` Causes doRun.run() to be executed synchronously on the AWT event dispatching thread.
`static void` | `invokeLater(java.lang.Runnable doRun)` Causes doRun.run() to be executed asynchronously on the AWT event dispatching thread.
`static boolean` | `isDescendingFrom(java.awt.Component a, java.awt.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(java.awt.event.MouseEvent anEvent)` Returns true if the mouse event specifies the left mouse button.
`static boolean` | `isMiddleMouseButton(java.awt.event.MouseEvent anEvent)` Returns true if the mouse event specifies the middle mouse button.
`static boolean` | `isRectangleContainingRectangle(java.awt.Rectangle a, java.awt.Rectangle b)` Return true if `a` contains `b`
`static boolean` | `isRightMouseButton(java.awt.event.MouseEvent anEvent)` Returns true if the mouse event specifies the right mouse button.
`static java.lang.String` | `layoutCompoundLabel(java.awt.FontMetrics fm, java.lang.String text, Icon icon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, java.awt.Rectangle viewR, java.awt.Rectangle iconR, java.awt.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 java.lang.String` | `layoutCompoundLabel(JComponent c, java.awt.FontMetrics fm, java.lang.String text, Icon icon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, java.awt.Rectangle viewR, java.awt.Rectangle iconR, java.awt.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 void` | `paintComponent(java.awt.Graphics g, java.awt.Component c, java.awt.Container p, int x, int y, int w, int h)` Paint a component c on an abitrary graphics g in the specified rectangle, specifying the rectangle's upper left corner and size.
`static void` | `paintComponent(java.awt.Graphics g, java.awt.Component c, java.awt.Container p, java.awt.Rectangle r)` Paint a component c on an abitrary graphics g in the specified rectangle, specifying a Rectangle object.
`static void` | `updateComponentTreeUI(java.awt.Component c)` A simple minded look and feel change: ask each node in the tree to updateUI(), i.e.
`static java.awt.Window` | `windowForComponent(java.awt.Component aComponent)` Return `aComponent`'s window

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Method Detail

### isRectangleContainingRectangle

    public static final boolean isRectangleContainingRectangle(java.awt.Rectangle a,
                                                               java.awt.Rectangle b)

  Return true if `a` contains `b`

### getLocalBounds

    public static java.awt.Rectangle getLocalBounds(java.awt.Component aComponent)

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

### convertPoint

    public static java.awt.Point convertPoint(java.awt.Component source,
                                              java.awt.Point aPoint,
                                              java.awt.Component destination)

  Convert a `aPoint` in `source` coordinate system to `destination` coordinate system. If `source>`is null,`aPoint` is assumed to be in `destination`'s root component coordinate system. If `destination`is null, `aPoint` will be converted to `source`'s root component coordinate system. If both `source` and `destination` are null, return `aPoint` without any conversion.

### convertPoint

    public static java.awt.Point convertPoint(java.awt.Component source,
                                              int x,
                                              int y,
                                              java.awt.Component destination)

  Convert the point `(x,y)` in `source` coordinate system to `destination` coordinate system. If `source>`is null,`(x,y)` is assumed to be in `destination`'s root component coordinate system. If `destination`is null, `(x,y)` will be converted to `source`'s root component coordinate system. If both `source` and `destination` are null, return `(x,y)` without any conversion.

### convertRectangle

    public static java.awt.Rectangle convertRectangle(java.awt.Component source,
                                                      java.awt.Rectangle aRectangle,
                                                      java.awt.Component destination)

  Convert the rectangle `aRectangle` in `source` coordinate system to `destination` coordinate system. If `source>`is null,`aRectangle` is assumed to be in `destination`'s root component coordinate system. If `destination`is null, `aRectangle` will be converted to `source`'s root component coordinate system. If both `source` and `destination` are null, return `aRectangle` without any conversion.

### getAncestorOfClass

    public static java.awt.Container getAncestorOfClass(java.lang.Class c,
                                                        java.awt.Component comp)

  Convenience method for searching above `comp` in the component hierarchy and returns the first object of class `c` it finds. Can return null, if a class `c` cannot be found.

### getAncestorNamed

    public static java.awt.Container getAncestorNamed(java.lang.String name,
                                                      java.awt.Component comp)

  Convenience method for searching above `comp` in the component hierarchy and returns the first object of `name` it finds. Can return null, if `name` cannot be found.

### getDeepestComponentAt

    public static java.awt.Component getDeepestComponentAt(java.awt.Component parent,
                                                           int x,
                                                           int y)

  Returns the deepest visible descendent Component of `parent` that contains the location `x`, `y`. If `parent` does not contain the specified location, then `null` is returned. If `parent` is not a container, or none of `parent`'s visible descendents contain the specified location, `parent` is returned.
  Parameters:
    `parent` - the root component to begin the search
    `x` - the x target location
    `y` - the y target location

### convertMouseEvent

    public static java.awt.event.MouseEvent convertMouseEvent(java.awt.Component source,
                                                              java.awt.event.MouseEvent sourceEvent,
                                                              java.awt.Component destination)

  Returns a MouseEvent similar to `sourceEvent` except that its x and y members have been converted to `destination`'s coordinate system. If `source` is null, `sourceEvent` x and y members are assumed to be into `destination`'s root component coordinate system. If `destination` is `null`, the returned MouseEvent will be in `source`'s coordinate system. `sourceEvent` will not be changed. A new event is returned. the `source` field of the returned event will be set to `destination` if destination is non null use the translateMouseEvent() method to translate a mouse event from one component to another without changing the source.``

### convertPointToScreen

    public static void convertPointToScreen(java.awt.Point p,
                                            java.awt.Component c)

  Convert a point from a component's coordinate system to screen coordinates.
  Parameters:
    `p` - a Point object (converted to the new coordinate system)
    `c` - a Component object

### convertPointFromScreen

    public static void convertPointFromScreen(java.awt.Point p,
                                              java.awt.Component c)

  Convert a point from a screen coordinates to a component's coordinate system
  Parameters:
    `p` - a Point object (converted to the new coordinate system)
    `c` - a Component object

### windowForComponent

    public static java.awt.Window windowForComponent(java.awt.Component aComponent)

  Return `aComponent`'s window

### isDescendingFrom

    public static boolean isDescendingFrom(java.awt.Component a,
                                           java.awt.Component b)

  Return `true` if a component `a` descends from a component `b`

### computeIntersection

    public static java.awt.Rectangle computeIntersection(int x,
                                                         int y,
                                                         int width,
                                                         int height,
                                                         java.awt.Rectangle dest)

  Convenience to calculate an intersection of two rectangles without allocating a new rectangle Return dest.

### computeUnion

    public static java.awt.Rectangle computeUnion(int x,
                                                  int y,
                                                  int width,
                                                  int height,
                                                  java.awt.Rectangle dest)

  Convenience to calculate the union of two rectangles without allocating a new rectangle Return dest

### computeDifference

    public static java.awt.Rectangle[] computeDifference(java.awt.Rectangle rectA,
                                                         java.awt.Rectangle rectB)

  Convenience returning an array of rect representing the regions within `rectA` that do not overlap with `rectB`. If the two Rects do not overlap, returns an empty array

### isLeftMouseButton

    public static boolean isLeftMouseButton(java.awt.event.MouseEvent anEvent)

  Returns true if the mouse event specifies the left mouse button.
  Parameters:
    `anEvent` - a MouseEvent object
  Returns:
    true if the left mouse button was active

### isMiddleMouseButton

    public static boolean isMiddleMouseButton(java.awt.event.MouseEvent anEvent)

  Returns true if the mouse event specifies the middle mouse button.
  Parameters:
    `anEvent` - a MouseEvent object
  Returns:
    true if the middle mouse button was active

### isRightMouseButton

    public static boolean isRightMouseButton(java.awt.event.MouseEvent anEvent)

  Returns true if the mouse event specifies the right mouse button.
  Parameters:
    `anEvent` - a MouseEvent object
  Returns:
    true if the right mouse button was active

### computeStringWidth

    public static int computeStringWidth(java.awt.FontMetrics fm,
                                         java.lang.String str)

  Compute the width of the string using a font with the specified "metrics" (sizes).
  Parameters:
    `fm` - a FontMetrics object to compute with
    `str` - the String to compute
  Returns:
    an int containing the string width

### layoutCompoundLabel

    public static java.lang.String layoutCompoundLabel(JComponent c,
                                                       java.awt.FontMetrics fm,
                                                       java.lang.String text,
                                                       Icon icon,
                                                       int verticalAlignment,
                                                       int horizontalAlignment,
                                                       int verticalTextPosition,
                                                       int horizontalTextPosition,
                                                       java.awt.Rectangle viewR,
                                                       java.awt.Rectangle iconR,
                                                       java.awt.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. Locations are computed relative to the viewR rectangle. The JComponents orientation (LEADING/TRAILING) will also be taken into account and translated into LEFT/RIGHT values accordingly.

### layoutCompoundLabel

    public static java.lang.String layoutCompoundLabel(java.awt.FontMetrics fm,
                                                       java.lang.String text,
                                                       Icon icon,
                                                       int verticalAlignment,
                                                       int horizontalAlignment,
                                                       int verticalTextPosition,
                                                       int horizontalTextPosition,
                                                       java.awt.Rectangle viewR,
                                                       java.awt.Rectangle iconR,
                                                       java.awt.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. Locations are computed relative to the viewR rectangle. This layoutCompoundLabel() does not know how to handle LEADING/TRAILING values in horizontalTextPosition (they will default to RIGHT) and in horizontalAlignment (they will default to CENTER). Use the other version of layoutCompoundLabel() instead.

### paintComponent

    public static void paintComponent(java.awt.Graphics g,
                                      java.awt.Component c,
                                      java.awt.Container p,
                                      int x,
                                      int y,
                                      int w,
                                      int h)

  Paint a component c on an abitrary graphics g in the specified rectangle, specifying the rectangle's upper left corner and size. The component is reparented to a private container (whose parent becomes p) which prevents c.validate() and and c.repaint() calls from propogating up the tree. The intermediate container has no other effect.
  Parameters:
    `g` - the Graphics object to draw on
    `c` - the Component to draw
    `p` - the intermedate Container
    `x` - an int specifying the left side of the area draw in, in pixels, measured from the left edge of the graphics context
    `y` - an int specifying the top of the area to draw in, in pixels measured down from the top edge of the graphics context
    `w` - an int specifying the width of the area draw in, in pixels
    `h` - an int specifying the height of the area draw in, in pixels

### paintComponent

    public static void paintComponent(java.awt.Graphics g,
                                      java.awt.Component c,
                                      java.awt.Container p,
                                      java.awt.Rectangle r)

  Paint a component c on an abitrary graphics g in the specified rectangle, specifying a Rectangle object. The component is reparented to a private container (whose parent becomes p) which prevents c.validate() and and c.repaint() calls from propogating up the tree. The intermediate container has no other effect.
  Parameters:
    `g` - the Graphics object to draw on
    `c` - the Component to draw
    `p` - the intermedate Container
    `r` - the Rectangle to draw in

### updateComponentTreeUI

    public static void updateComponentTreeUI(java.awt.Component c)

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

### invokeLater

    public static void invokeLater(java.lang.Runnable doRun)

  Causes doRun.run() to be executed asynchronously on the AWT event dispatching thread. This will happen after all pending AWT events have been processed. This method should be used when an application thread needs to update the GUI. In the following example the invokeAndWait() calls queues the doHelloWorld Runnable for the event dispatching thread and then prints a message.

       Runnable doHelloWorld = new Runnable() {
           public void run() {
               System.out.println("Hello World on " + Thread.currentThread());
           }
       };

       SwingUtilities.invokeAndWait(doHelloWorld);
       System.out.println("Waiting ... ");

  If invokeAndWait is called from the event dispatching thread, e.g. from a JButtons ActionListener, the doRun.run() will still be deferred till 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 .

  See Also:
    `invokeAndWait(java.lang.Runnable)`

### invokeAndWait

    public static void invokeAndWait(java.lang.Runnable doRun)
                              throws java.lang.InterruptedException,
                                     java.lang.reflect.InvocationTargetException

  Causes doRun.run() to be executed synchronously on the AWT event dispatching thread. This call will block until all pending AWT events have been processed and (then) doRun.run() returns. This method should be used when an application thread needs to update the GUI. It should not be called from the EventDispatchThread. Here's an example that creates a new application thread that uses invokeAndWait() to print a string from the event dispatching thread and then, when that's finished, print a string from the application thread.

       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 callers thread.

  Additional documentation and examples for this method can be found in .

  Throws:
    java.lang.InterruptedException - If we're interrupted while waiting for the event dispatching thread to finish excecuting doRun.run()
    java.lang.reflect.InvocationTargetException - If doRun.run() throws
  See Also:
    `invokeLater(java.lang.Runnable)`

### isEventDispatchThread

    public static boolean isEventDispatchThread()

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

### getAccessibleIndexInParent

    public static int getAccessibleIndexInParent(java.awt.Component c)

  Get the index of this object in its accessible parent.
  Returns:
    -1 of this object does not have an accessible parent. Otherwise, the index of the child in its accessible parent.

### getAccessibleAt

    public static Accessible getAccessibleAt(java.awt.Component c,
                                             java.awt.Point p)

  Returns the Accessible child contained at the local coordinate Point, if one exists.
  Returns:
    the Accessible at the specified location, if it exists

### getAccessibleStateSet

    public static AccessibleStateSet getAccessibleStateSet(java.awt.Component c)

  Get the state of this object.
  Returns:
    an instance of AccessibleStateSet containing the current state set of the object
  See Also:
    `AccessibleState`

### getAccessibleChildrenCount

    public static int getAccessibleChildrenCount(java.awt.Component c)

  Returns the number of accessible children in the object. If all of the children of this object implement Accessible, than this method should return the number of children of this object.
  Returns:
    the number of accessible children in the object.

### getAccessibleChild

    public static Accessible getAccessibleChild(java.awt.Component c,
                                                int i)

  Return the nth Accessible child of the object.
  Parameters:
    `i` - zero-based index of child
  Returns:
    the nth Accessible child of the object

### findFocusOwner

    public static java.awt.Component findFocusOwner(java.awt.Component c)

  Return the child component which has focus, if any. The HotJava SecurityManager forbids applet access to getFocusOwner(), so if the component is an applet, we check whether a JComponent has focus. Non-Swing components in an applet on HotJava are out-of-luck, unfortunately.

### getRootPane

    public static JRootPane getRootPane(java.awt.Component c)

  If c is a JRootPane descendant return its JRootPane ancestor. If c is a RootPaneContainer then return its JRootPane.
  Returns:
    the JRootPane for Component c or null.

### getRoot

    public static java.awt.Component getRoot(java.awt.Component c)

  Returns the root component for the current component tree.
  Returns:
    the first ancestor of c that's a Window or the last Applet ancestor

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| Swing 1.1
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Submit a bug or feature
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1998 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.