|
JSR-209 (Final Release) | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjavax.swing.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(java.awt.FontMetrics fm,
java.lang.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 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 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 Point |
convertPoint(java.awt.Component source,
Point aPoint,
java.awt.Component destination)
Convert a aPoint in source coordinate system to
destination coordinate system. |
static void |
convertPointFromScreen(Point p,
java.awt.Component c)
Convert a point from a screen coordinates to a component's coordinate system |
static void |
convertPointToScreen(Point p,
java.awt.Component c)
Convert a point from a component's coordinate system to screen coordinates. |
static Rectangle |
convertRectangle(java.awt.Component source,
Rectangle aRectangle,
java.awt.Component destination)
Convert the rectangle aRectangle in source coordinate system to
destination coordinate system. |
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 Rectangle |
getLocalBounds(java.awt.Component aComponent)
Return the rectangle (0,0,bounds.width,bounds.height) for the component aComponent |
static JRootPane |
getRootPane(java.awt.Component c)
If c is a JRootPane descendant return its JRootPane ancestor. |
static java.awt.Window |
getWindowAncestor(java.awt.Component c)
|
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(Rectangle a,
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,
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,
java.awt.event.KeyEvent event,
java.lang.Object sender,
int modifiers)
Invokes actionPerformed on action if
action is enabled (and non null). |
static boolean |
processKeyBindings(java.awt.event.KeyEvent event)
Process the key bindings for the Component associated with
event . |
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)
a
contains b
public static Rectangle getLocalBounds(java.awt.Component aComponent)
aComponent
public static java.awt.Window getWindowAncestor(java.awt.Component c)
public static Point convertPoint(java.awt.Component source, Point aPoint, java.awt.Component destination)
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.
public static Point convertPoint(java.awt.Component source, int x, int y, java.awt.Component destination)
(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.
public static Rectangle convertRectangle(java.awt.Component source, Rectangle aRectangle, java.awt.Component destination)
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.
public static java.awt.Container getAncestorOfClass(java.lang.Class c, java.awt.Component comp)
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.
public static java.awt.Container getAncestorNamed(java.lang.String name, java.awt.Component comp)
comp
in the
component hierarchy and returns the first object of name
it
finds. Can return null, if name
cannot be found.
public static java.awt.Component getDeepestComponentAt(java.awt.Component parent, int x, int y)
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.
parent
- the root component to begin the searchx
- the x target locationy
- the y target locationpublic static java.awt.event.MouseEvent convertMouseEvent(java.awt.Component source, java.awt.event.MouseEvent sourceEvent, java.awt.Component destination)
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.
public static void convertPointToScreen(Point p, java.awt.Component c)
p
- a Point object (converted to the new coordinate system)c
- a Component objectpublic static void convertPointFromScreen(Point p, java.awt.Component c)
p
- a Point object (converted to the new coordinate system)c
- a Component objectpublic static boolean isDescendingFrom(java.awt.Component a, java.awt.Component b)
true
if a component a
descends from a component b
public static Rectangle computeIntersection(int x, int y, int width, int height, Rectangle dest)
x
- the X coordinate of the first rectangle's top-left pointy
- the Y coordinate of the first rectangle's top-left pointwidth
- the width of the first rectangleheight
- the height of the first rectangledest
- the second rectangle
dest
, modified to specify the intersectionpublic static Rectangle computeUnion(int x, int y, int width, int height, Rectangle dest)
x
- the x-coordinate of the first rectangley
- the y-coordinate of the first rectanglewidth
- the width of the first rectangleheight
- the height of the first rectangledest
- the coordinates of the second rectangle; the union
of the two rectangles is returned in this rectangle
dest
Rectangle
public static Rectangle[] computeDifference(Rectangle rectA, Rectangle rectB)
rectA
that do not overlap with rectB
. If the
two Rects do not overlap, returns an empty array
public static boolean isLeftMouseButton(java.awt.event.MouseEvent anEvent)
anEvent
- a MouseEvent object
public static boolean isMiddleMouseButton(java.awt.event.MouseEvent anEvent)
anEvent
- a MouseEvent object
public static boolean isRightMouseButton(java.awt.event.MouseEvent anEvent)
anEvent
- a MouseEvent object
public static int computeStringWidth(java.awt.FontMetrics fm, java.lang.String str)
fm
- a FontMetrics object to compute withstr
- the String to compute
public static java.lang.String layoutCompoundLabel(java.awt.FontMetrics fm, java.lang.String text, Icon icon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewR, Rectangle iconR, Rectangle textR, int textIconGap)
public static void invokeLater(java.lang.Runnable doRun)
invokeLater
call queues
the Runnable
object doHelloWorld
on 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.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()
.
invokeAndWait(java.lang.Runnable)
public static void invokeAndWait(java.lang.Runnable doRun) throws java.lang.InterruptedException, java.lang.reflect.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 callers 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()
.
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() throwsinvokeLater(java.lang.Runnable)
public static boolean isEventDispatchThread()
As of 1.3 this method is just a cover for
java.awt.EventQueue.isDispatchThread()
.
public static JRootPane getRootPane(java.awt.Component c)
public static boolean processKeyBindings(java.awt.event.KeyEvent event)
Component
associated with
event
. This method is only useful if
event.getComponent()
does not descend from
JComponent
, or your are not invoking
super.processKeyEvent
from within your
JComponent
subclass. JComponent
automatically processes bindings from within its
processKeyEvent
method, hence you rarely need
to directly invoke this method.
event
- KeyEvent used to identify which bindings to process, as
well as which Component has focus.
public static boolean notifyAction(Action action, KeyStroke ks, java.awt.event.KeyEvent event, java.lang.Object sender, int modifiers)
actionPerformed
on action
if
action
is enabled (and non null). The command for the
ActionEvent is determined by:
registerKeyboardAction
, then the command string
passed in (null will be used if null was passed in).
getKeyChar
returns KeyEvent.CHAR_UNDEFINED..
action
is non-null and
actionPerformed is invoked on it.
public static Rectangle calculateInnerArea(JComponent c, Rectangle r)
r
and returns r
.
The position and size specify the bounds of the component,
adjusted so as not to include the border area (the insets).
This method is useful for classes
that implement painting code.
c
- the JComponent in question; if null, this method returns nullr
- the Rectangle instance to be modified;
may be null
|
JSR-209 (Final Release) | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |