Top Description Inners Fields Constructors Methods
sun.swing

public Class SwingUtilities2

extends Object
Class Inheritance
Imports
java.awt.AWTEvent, .Color, .Component, .Container, .Dimension, .EventQueue, .FocusTraversalPolicy, .Font, .FontMetrics, .Graphics, .Graphics2D, .GraphicsConfiguration, .GraphicsEnvironment, .Point, .PrintGraphics, .Rectangle, .RenderingHints, .Shape, .Toolkit, java.awt.event.InputEvent, .KeyEvent, .MouseEvent, java.awt.font.FontRenderContext, .GlyphVector, .LineBreakMeasurer, .TextAttribute, .TextHitInfo, .TextLayout, java.awt.geom.AffineTransform, .Rectangle2D, java.awt.print.PrinterGraphics, java.beans.PropertyChangeEvent, java.io.BufferedInputStream, .IOException, .InputStream, java.lang.reflect.Modifier, java.security.AccessController, .PrivilegedAction, java.text.AttributedCharacterIterator, .AttributedString, .BreakIterator, .CharacterIterator, java.util.HashMap, .Locale, .Map, .Objects, java.util.concurrent.Callable, .Future, .FutureTask, javax.swing.JComponent, .JList, .JTable, .ListCellRenderer, .ListSelectionModel, .SwingUtilities, .UIDefaults, .UIManager, javax.swing.event.TreeModelEvent, javax.swing.table.TableCellRenderer, .TableColumnModel, javax.swing.text.DefaultCaret, .DefaultHighlighter, .Highlighter, .JTextComponent, javax.swing.tree.TreeModel, .TreePath, sun.awt.AWTAccessor, .AWTPermissions, .AppContext, .SunToolkit, sun.font.FontDesignMetrics, .FontUtilities, sun.java2d.SunGraphicsEnvironment, sun.print.ProxyPrintGraphics

A collection of utility methods for Swing.

Warning

While this class is public, it should not be treated as public API and its API may change in incompatible ways between dot dot releases and even patch releases. You should not rely on this class even existing.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
SwingUtilities2.LSBCacheEntry

LSBCacheEntry is used to cache the left side bearing (lsb) for a particular Font and FontRenderContext.

public static interface
SwingUtilities2.RepaintListener

Used to listen to "blit" repaints in RepaintManager.

public static enum
SwingUtilities2.Section

Used by the liesIn method to return which section the point lies in.

Field Summary

Modifier and TypeField and Description
private static final Object
public static final StringUIClientPropertyKey
BASICMENUITEMUI_MAX_TEXT_OFFSET

Client Property key for the text maximal offsets for BasicMenuItemUI

private static final int
private static final int
private static char[]
private static final Object
public static final FontRenderContext
private static SwingUtilities2.LSBCacheEntry[]
public static final String
IMPLIED_CR

Attribute key for the content elements.

public static final Object
LAF_STATE_KEY

The AppContext key for our one LAFState instance.

private static final int
private static final int
private static int
private static SwingUtilities2.LSBCacheEntry
private static final StringBuilder
SKIP_CLICK_COUNT

Used to tell a text component, being used as an editor for table or tree, how many clicks it took to start editing.

private static final String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static void
adjustFocus(JComponent c)

Request focus on the given component if it doesn't already have it and isRequestFocusEnabled() returns true.

public static boolean
canAccessSystemClipboard()

checks the security permissions for accessing system clipboard for untrusted context (see isTrustedContext) checks the permissions for the current event being handled

public static boolean
canCurrentEventAccessSystemClipboard()

Returns true if EventQueue.getCurrentEvent() has the permissions to access the system clipboard

private static boolean
canCurrentEventAccessSystemClipboard(boolean
boolean
checkGesture
)

Returns true if EventQueue.getCurrentEvent() has the permissions to access the system clipboard and if it is allowed gesture (if checkGesture true)

public static boolean
canEventAccessSystemClipboard(AWTEvent
AWTEvent to check
e
)

Returns true if the given event has permissions to access the system clipboard

private static boolean
canEventAccessSystemClipboard(AWTEvent
AWTEvent to check
e
,
boolean
boolean
checkGesture
)

Returns true if e has the permissions to access the system clipboard and if it is allowed gesture (if checkGesture is true)

public static void
checkAccess(int
a set of modifiers
modifiers
)

Utility method that throws SecurityException if SecurityManager is set and modifiers are not public

public static String

Returns:

Clipped string that can fit in the provided space.
clipString
(JComponent
JComponent that will display the string, may be null
c
,
FontMetrics
FontMetrics used to measure the String width
fm
,
String
String to display
string
,
int
Amount of space that the string can be drawn in
availTextWidth
)

Clips the passed in String to the space provided.

public static String

Returns:

Clipped string that can fit in the provided space.
clipStringIfNecessary
(JComponent
JComponent that will display the string, may be null
c
,
FontMetrics
FontMetrics used to measure the String width
fm
,
String
String to display
string
,
int
Amount of space that the string can be drawn in
availTextWidth
)

Clips the passed in String to the space provided.

public static Component
public static int

Returns:

the index of the corresponding column in the model
convertColumnIndexToModel
(TableColumnModel
the table model
cm
,
int
the index of the column in the view
viewColumnIndex
)

Maps the index of the column in the view at viewColumnIndex to the index of the column in the table model.

public static int

Returns:

the index of the corresponding column in the view
convertColumnIndexToView
(TableColumnModel
the table model
cm
,
int
the index of the column in the model
modelColumnIndex
)

Maps the index of the column in the cm at modelColumnIndex to the index of the column in the view.

private static TextLayout
public static String
public static int
drawChars(JComponent c, Graphics g, char[] data, int offset, int length, int x, int y)

The following draw functions have the same semantic as the Graphics methods with the same names.

public static float
drawChars(JComponent c, Graphics g, char[] data, int offset, int length, float x, float y)

public static float
drawChars(JComponent c, Graphics g, char[] data, int offset, int length, float x, float y, boolean useFPAPI)

public static void
drawHLine(Graphics
Graphics to draw the line to.
g
,
int
the first point's x coordinate.
x1
,
int
the second point's x coordinate.
x2
,
int
the y coordinate.
y
)

This method should be used for drawing a borders over a filled rectangle.

public static void
drawRect(Graphics
Graphics to draw the rectangle to.
g
,
int
the x coordinate of the rectangle to be drawn.
x
,
int
the y coordinate of the rectangle to be drawn.
y
,
int
the w of the rectangle to be drawn.
w
,
int
the h of the rectangle to be drawn.
h
)

This method should be used for drawing a borders over a filled rectangle.

public static void
drawString(JComponent
JComponent that will display the string, may be null
c
,
Graphics
Graphics to draw the text to
g
,
String
String to display
text
,
int
X coordinate to draw the text at
x
,
int
Y coordinate to draw the text at
y
)

Draws the string at the specified location.

public static void
drawString(JComponent
JComponent that will display the string, may be null
c
,
Graphics
Graphics to draw the text to
g
,
String
String to display
text
,
float
X coordinate to draw the text at
x
,
float
Y coordinate to draw the text at
y
,
boolean
use floating point API
useFPAPI
)

Draws the string at the specified location.

public static float
public static float
drawString(JComponent c, Graphics g, AttributedCharacterIterator iterator, float x, float y)

private static float
public static void
drawStringUnderlineCharAt(JComponent
JComponent that will display the string, may be null
c
,
Graphics
Graphics to draw the text to
g
,
String
String to display
text
,
int
Index of a character in the string to underline
underlinedIndex
,
int
X coordinate to draw the text at
x
,
int
Y coordinate to draw the text at
y
)

Draws the string at the specified location underlining the specified character.

public static void
drawStringUnderlineCharAt(JComponent
JComponent that will display the string, may be null
c
,
Graphics
Graphics to draw the text to
g
,
String
String to display
text
,
int
Index of a character in the string to underline
underlinedIndex
,
float
X coordinate to draw the text at
x
,
float
Y coordinate to draw the text at
y
,
boolean
use floating point API
useFPAPI
)

Draws the string at the specified location underlining the specified character.

public static void
drawVLine(Graphics
Graphics to draw the line to.
g
,
int
the x coordinate.
x
,
int
the first point's y coordinate.
y1
,
int
the second point's y coordinate.
y2
)

This method should be used for drawing a borders over a filled rectangle.

private static void
execute(Runnable command)

Sends a Runnable to the EDT for the execution.

public static int
getAdjustedClickCount(JTextComponent comp, MouseEvent e)

Return the MouseEvent's click count, possibly reduced by the value of the component's SKIP_CLICK_COUNT client property.

public static Object

Returns:

the previous antialiasing rendering hint value if the passed graphics is instance of Graphics2D, null otherwise.
getAndSetAntialisingHintForScaledGraphics
(Graphics
the graphics
g
)

Enables the antialiasing rendering hint for the scaled graphics and returns the previous hint value.

public static boolean

Returns:

the boolean value of the client property if it is set or the L&F property in other case.
getBoolean
(JComponent
the component
component
,
String
an String specifying the key for the desired boolean value
key
)

Returns the client property for the given key if it is set; otherwise returns the L&F; property.

public static float
getFontCharsWidth(char[] data, int offset, int len, FontMetrics fm, boolean useFPAPI)

public static float
getFontCharWidth(char c, FontMetrics fm, boolean useFPAPI)

public static FontMetrics
getFontMetrics(JComponent
JComponent requesting FontMetrics, may be null
c
,
Graphics
Graphics Graphics
g
)

Returns the FontMetrics for the current Font of the passed in Graphics.

public static FontMetrics
getFontMetrics(JComponent
JComponent requesting FontMetrics, may be null
c
,
Graphics
Graphics
g
,
Font
Font to get FontMetrics for
font
)

Returns the FontMetrics for the specified Font.

public static FontMetrics
public static FontRenderContext
private static FontRenderContext
getFontRenderContext(Component c, FontMetrics fm)

A convenience method to get FontRenderContext.

public static float
getFontStringWidth(String data, FontMetrics fm, boolean useFPAPI)

private static FontRenderContext
private static FontRenderContext
public static Graphics2D
private static byte[]
getIconBytes(final Class<?> baseClass, final Class<?> rootClass, final String imageFile)

public static int

Returns:

the left side bearing of the first character of string or 0 if the string is empty
getLeftSideBearing
(JComponent
JComponent that will display the string
c
,
FontMetrics
FontMetrics used to measure the String width
fm
,
String
String to get the left side bearing for.
string
)

Returns the left side bearing of the first character of string.

public static int
getLeftSideBearing(JComponent
JComponent that will display the string
c
,
FontMetrics
FontMetrics used to measure the String width
fm
,
char
Character to get the left side bearing for.
firstChar
)

Returns the left side bearing of the first character of string.

public static int
public static TreePath

Returns:

the path to the changed nodes
getTreePath
(TreeModelEvent
changes in a tree model
event
,
TreeModel
corresponding tree model
model
)

Returns the TreePath that identifies the changed nodes.

private static AttributedCharacterIterator
public static int

Returns:

the int
getUIDefaultsInt
(Object
an Object specifying the int.
key
)

Returns an integer from the defaults table.

public static int

Returns:

the int
getUIDefaultsInt
(Object
an Object specifying the int. Returned value is 0 if key is not available,
key
,
Locale
the Locale for which the int is desired
l
)

Returns an integer from the defaults table that is appropriate for the given locale.

public static int

Returns:

the int
getUIDefaultsInt
(Object
an Object specifying the int. Returned value is 0 if key is not available,
key
,
int
Returned value if key is not available, or is not an Integer
defaultValue
)

Returns an integer from the defaults table.

public static int

Returns:

the int
getUIDefaultsInt
(Object
an Object specifying the int. Returned value is 0 if key is not available,
key
,
Locale
the Locale for which the int is desired
l
,
int
Returned value if key is not available, or is not an Integer
defaultValue
)

Returns an integer from the defaults table that is appropriate for the given locale.

private static boolean
isAccessClipboardGesture(InputEvent
InputEvent to check
ie
)

Returns true if the given event is current gesture for accessing clipboard

public static final boolean

Returns:

true if TextLayout is required false if TextLayout is not required
isComplexLayout
(char[]
characters to be tested
text
,
int
start
start
,
int
limit
limit
)

checks whether TextLayout is required to handle characters.

public static boolean
private static boolean
public static boolean
pack-priv static boolean
public static boolean

Returns:

whether or not the scale was changed
isScaleChanged
(final PropertyChangeEvent
a PropertyChangeEvent
ev
)

Returns whether or not the scale used by GraphicsConfiguration was changed.

public static boolean

Returns:

whether or not the scale was changed
isScaleChanged
(final String
the name of the property
name
,
final Object
the old value of the property
oldValue
,
final Object
the new value of the property
newValue
)

Returns whether or not the scale used by GraphicsConfiguration was changed.

public static boolean
private static boolean
isTrustedContext()

see RFE 5012841 [Per AppContect security permissions] for the details

private static SwingUtilities2.Section

Returns:

the Section where the point lies
liesIn
(Rectangle
the rectangle
rect
,
Point
the point the check
p
,
boolean
true to use the horizontal axis, or false for the vertical axis
horizontal
,
boolean
true for left to right orientation, or false for right to left orientation; only used for horizontal calculations
ltr
,
boolean
true for three sections, or false for two
three
)

This method divides a rectangle into two or three sections along the specified axis and determines which section the given point lies in on that axis; used by drag and drop when calculating drop locations.

public static SwingUtilities2.Section

Returns:

the Section where the point lies
liesInHorizontal
(Rectangle
the rectangle
rect
,
Point
the point the check
p
,
boolean
true for left to right orientation, or false for right to left orientation
ltr
,
boolean
true for three sections, or false for two
three
)

This method divides a rectangle into two or three sections along the horizontal axis and determines which section the given point lies in; used by drag and drop when calculating drop locations.

public static SwingUtilities2.Section

Returns:

the Section where the point lies
liesInVertical
(Rectangle
the rectangle
rect
,
Point
the point the check
p
,
boolean
true for three sections, or false for two
three
)

This method divides a rectangle into two or three sections along the vertical axis and determines which section the given point lies in; used by drag and drop when calculating drop locations.

public static int
loc2IndexFileList(JList<?> list, Point point)

A variation of locationToIndex() which only returns an index if the Point is within the actual bounds of a list item (not just in the cell) and if the JList has the "List.isFileList" client property set.

public static Object

Returns:

a lazy value that creates the ImageIcon UIResource for the image, or null if it cannot be found
makeIcon
(final Class<?>
the first class to use in searching for the resource
baseClass
,
final Class<?>
an ancestor of baseClass to finish the search at
rootClass
,
final String
the name of the file to be found
imageFile
)

Utility method that creates a UIDefaults.LazyValue that creates an ImageIcon UIResource for the specified image file name.

private static Object
makeIcon(final Class<?> baseClass, final Class<?> rootClass, final String imageFile, final boolean enablePrivileges)

public static Object

Returns:

a lazy value that creates the ImageIcon UIResource for the image, or null if it cannot be found
makeIcon_Unprivileged
(final Class<?>
the first class to use in searching for the resource
baseClass
,
final Class<?>
an ancestor of baseClass to finish the search at
rootClass
,
final String
the name of the file to be found
imageFile
)

Utility method that creates a UIDefaults.LazyValue that creates an ImageIcon UIResource for the specified image file name.

private static <T> boolean
pointIsInActualBounds(JList<T> list, int index, Point point)

Returns true if the given point is within the actual bounds of the JList item at index (not just inside the cell).

public static boolean
pointOutsidePrefSize(JTable table, int row, int column, Point p)

Returns true if the given point is outside the preferredSize of the item at the given row of the table.

public static void
putAATextInfo(boolean lafCondition, Map<Object, Object> map)

public static int

Returns:

the modifier returned with ALT_GRAPH flag set
setAltGraphMask
(int
the modifier passed
modifier
)

Sets the InputEvent.ALT_GRAPH mask on any modifier passed to the function

public static void
setAntialiasingHintForScaledGraphics(Graphics
the graphics
g
,
Object
the antialiasing rendering hint
hint
)

Sets the antialiasing rendering hint if its value is not null.

public static void
setLeadAnchorWithoutSelection(ListSelectionModel model, int lead, int anchor)

Set the lead and anchor without affecting selection.

public static void
setSkipClickCount(Component comp, int count)

Sets the SKIP_CLICK_COUNT client property on the component if it is an instance of JTextComponent with a DefaultCaret.

public static boolean
shouldIgnore(MouseEvent me, JComponent c)

Ignore mouse events if the component is null, not enabled, the event is not associated with the left mouse button, or the event has been consumed.

public static int
stringWidth(JComponent
JComponent that will display the string, may be null
c
,
FontMetrics
FontMetrics used to measure the String width
fm
,
String
String to get the width of
string
)

Returns the width of the passed in String.

public static float
stringWidth(JComponent
JComponent that will display the string, may be null
c
,
FontMetrics
FontMetrics used to measure the String width
fm
,
String
String to get the width of
string
,
boolean
use floating point API
useFPAPI
)

Returns the width of the passed in String.

public static <V> Future<V>

Returns:

a Future representing pending completion of the task
submit
(Callable<V>
the task to submit
task
)

Submits a value-returning task for execution on the EDT and returns a Future representing the pending results of the task.

public static <V> Future<V>

Returns:

a Future representing pending completion of the task, and whose get() method will return the given result value upon completion
submit
(Runnable
the task to submit
task
,
V
the result to return upon successful completion
result
)

Submits a Runnable task for execution on the EDT and returns a Future representing that task.

private static int
syncCharsBuffer(String s)

Fill the character buffer cache.

public static boolean
tabbedPaneChangeFocusTo(Component comp)

Change focus to the visible component in JTabbedPane.

public static boolean
useSelectedTextColor(Highlighter.Highlight h, JTextComponent c)

Determines whether the SelectedTextColor should be used for painting text foreground for the specified highlight.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait