public class

SwingUtilities2

extends Object
java.lang.Object
   ↳ sun.swing.SwingUtilities2

Class Overview

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 incompatable ways between dot dot releases and even patch releases. You should not rely on this class even existing.

Summary

Nested Classes
class SwingUtilities2.AATextInfo  
enum SwingUtilities2.Section Used by the liesIn method to return which section the point lies in. 
Fields
public static final Object AA_TEXT_PROPERTY_KEY A JComponent client property is used to determine text aa settings.
public static final StringUIClientPropertyKey BASICMENUITEMUI_MAX_TEXT_OFFSET Client Property key for the text maximal offsets for BasicMenuItemUI
public static final Object COMPONENT_UI_PROPERTY_KEY Key used in client properties used to indicate that the ComponentUI of the JComponent instance should be returned.
public static final FontRenderContext DEFAULT_FRC
public static final Object LAF_STATE_KEY The AppContext key for our one LAFState instance.
Public Constructors
SwingUtilities2()
Public Methods
static void adjustFocus(JComponent c)
Request focus on the given component if it doesn't already have it and isRequestFocusEnabled() returns true.
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
static boolean canCurrentEventAccessSystemClipboard()
Returns true if EventQueue.getCurrentEvent() has the permissions to access the system clipboard
static boolean canEventAccessSystemClipboard(AWTEvent e)
Returns true if the given event has permissions to access the system clipboard
static String clipString(JComponent c, FontMetrics fm, String string, int availTextWidth)
Clips the passed in String to the space provided.
static String clipStringIfNecessary(JComponent c, FontMetrics fm, String string, int availTextWidth)
Clips the passed in String to the space provided.
static Component compositeRequestFocus(Component component)
static String displayPropertiesToCSS(Font font, Color fg)
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.
static void drawString(JComponent c, Graphics g, String text, int x, int y)
Draws the string at the specified location.
static float drawString(JComponent c, Graphics g, AttributedCharacterIterator iterator, int x, int y)
static void drawStringUnderlineCharAt(JComponent c, Graphics g, String text, int underlinedIndex, int x, int y)
Draws the string at the specified location underlining the specified character.
static SwingUtilities2.AATextInfo drawTextAntialiased(JComponent c)
Returns whether or not text should be drawn antialiased.
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.
static FontMetrics getFontMetrics(JComponent c, Font font)
static FontMetrics getFontMetrics(JComponent c, Graphics g)
Returns the FontMetrics for the current Font of the passed in Graphics.
static FontMetrics getFontMetrics(JComponent c, Graphics g, Font font)
Returns the FontMetrics for the specified Font.
static FontRenderContext getFontRenderContext(Component c)
static Graphics2D getGraphics2D(Graphics g)
static int getLeftSideBearing(JComponent c, FontMetrics fm, char firstChar)
Returns the left side bearing of the specified character.
static int getLeftSideBearing(JComponent c, FontMetrics fm, String string)
Returns the left side bearing of the first character of string.
static int getRightSideBearing(JComponent c, FontMetrics fm, String string)
Returns the right side bearing of the last character of string.
static int getRightSideBearing(JComponent c, FontMetrics fm, char lastChar)
Returns the right side bearing of the specified character.
static int getUIDefaultsInt(Object key, Locale l)
Returns an integer from the defaults table that is appropriate for the given locale.
static int getUIDefaultsInt(Object key, Locale l, int defaultValue)
Returns an integer from the defaults table that is appropriate for the given locale.
static int getUIDefaultsInt(Object key)
Returns an integer from the defaults table.
static int getUIDefaultsInt(Object key, int defaultValue)
Returns an integer from the defaults table.
final static boolean isComplexLayout(char[] text, int start, int limit)
checks whether TextLayout is required to handle characters.
static boolean isLocalDisplay()
static SwingUtilities2.Section liesInHorizontal(Rectangle rect, Point p, boolean ltr, boolean 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.
static SwingUtilities2.Section liesInVertical(Rectangle rect, Point p, boolean 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.
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.
static Object makeIcon(Class<?> baseClass, Class<?> rootClass, String imageFile)
Utility method that creates a UIDefaults.LazyValue that creates an ImageIcon UIResource for the specified image file name.
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.
static void setLeadAnchorWithoutSelection(ListSelectionModel model, int lead, int anchor)
Set the lead and anchor without affecting selection.
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.
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.
static int stringWidth(JComponent c, FontMetrics fm, String string)
Returns the width of the passed in String.
static <V> Future<V> submit(Callable<V> task)
Submits a value-returning task for execution on the EDT and returns a Future representing the pending results of the task.
static <V> Future<V> submit(Runnable task, V result)
Submits a Runnable task for execution on the EDT and returns a Future representing that task.
static boolean tabbedPaneChangeFocusTo(Component comp)
Change focus to the visible component in JTabbedPane.
static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c)
Determines whether the SelectedTextColor should be used for painting text foreground for the specified highlight.
[Expand]
Inherited Methods
From class java.lang.Object

Fields

public static final Object AA_TEXT_PROPERTY_KEY

A JComponent client property is used to determine text aa settings. To avoid having this property persist between look and feels changes the value of the property is set to null in JComponent.setUI

public static final StringUIClientPropertyKey BASICMENUITEMUI_MAX_TEXT_OFFSET

Client Property key for the text maximal offsets for BasicMenuItemUI

public static final Object COMPONENT_UI_PROPERTY_KEY

Key used in client properties used to indicate that the ComponentUI of the JComponent instance should be returned.

public static final FontRenderContext DEFAULT_FRC

public static final Object LAF_STATE_KEY

The AppContext key for our one LAFState instance.

Public Constructors

public SwingUtilities2 ()

Public Methods

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

public static boolean canEventAccessSystemClipboard (AWTEvent e)

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

Parameters
e AWTEvent to check

public static String clipString (JComponent c, FontMetrics fm, String string, int availTextWidth)

Clips the passed in String to the space provided. NOTE: this assumes the string does not fit in the available space.

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

public static String clipStringIfNecessary (JComponent c, FontMetrics fm, String string, int availTextWidth)

Clips the passed in String to the space provided.

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

public static Component compositeRequestFocus (Component component)

public static String displayPropertiesToCSS (Font font, Color fg)

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. this is used for printing

public static void drawString (JComponent c, Graphics g, String text, int x, int y)

Draws the string at the specified location.

Parameters
c JComponent that will display the string, may be null
g Graphics to draw the text to
text String to display
x X coordinate to draw the text at
y Y coordinate to draw the text at

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

public static void drawStringUnderlineCharAt (JComponent c, Graphics g, String text, int underlinedIndex, int x, int y)

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

Parameters
c JComponent that will display the string, may be null
g Graphics to draw the text to
text String to display
underlinedIndex Index of a character in the string to underline
x X coordinate to draw the text at
y Y coordinate to draw the text at

public static SwingUtilities2.AATextInfo drawTextAntialiased (JComponent c)

Returns whether or not text should be drawn antialiased.

Parameters
c JComponent to test.
Returns
  • Whether or not text should be drawn antialiased for the specified component.

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. Clears the SKIP_CLICK_COUNT property if the mouse event's click count is 1. In order for clearing of the property to work correctly, there must be a mousePressed implementation on the caller with this call as the first line.

public static FontMetrics getFontMetrics (JComponent c, Font font)

public static FontMetrics getFontMetrics (JComponent c, Graphics g)

Returns the FontMetrics for the current Font of the passed in Graphics. This method is used when a Graphics is available, typically when painting. If a Graphics is not available the JComponent method of the same name should be used.

Callers should pass in a non-null JComponent, the exception to this is if a JComponent is not readily available at the time of painting.

This does not necessarily return the FontMetrics from the Graphics.

Parameters
c JComponent requesting FontMetrics, may be null
g Graphics Graphics

public static FontMetrics getFontMetrics (JComponent c, Graphics g, Font font)

Returns the FontMetrics for the specified Font. This method is used when a Graphics is available, typically when painting. If a Graphics is not available the JComponent method of the same name should be used.

Callers should pass in a non-null JComonent, the exception to this is if a JComponent is not readily available at the time of painting.

This does not necessarily return the FontMetrics from the Graphics.

Parameters
c Graphics Graphics
font Font to get FontMetrics for

public static FontRenderContext getFontRenderContext (Component c)

public static Graphics2D getGraphics2D (Graphics g)

public static int getLeftSideBearing (JComponent c, FontMetrics fm, char firstChar)

Returns the left side bearing of the specified character. The left side bearing is calculated from the passed in FontMetrics.

Parameters
c JComponent that will display the string
fm FontMetrics used to measure the String width
firstChar Character to get the left side bearing for.

public static int getLeftSideBearing (JComponent c, FontMetrics fm, String string)

Returns the left side bearing of the first character of string. The left side bearing is calculated from the passed in FontMetrics.

Parameters
c JComponent that will display the string
fm FontMetrics used to measure the String width
string String to get the left side bearing for.

public static int getRightSideBearing (JComponent c, FontMetrics fm, String string)

Returns the right side bearing of the last character of string. The right side bearing is calculated from the passed in FontMetrics.

Parameters
c JComponent that will display the string
fm FontMetrics used to measure the String width
string String to get the right side bearing for.

public static int getRightSideBearing (JComponent c, FontMetrics fm, char lastChar)

Returns the right side bearing of the specified character. The right side bearing is calculated from the passed in FontMetrics.

Parameters
c JComponent that will display the string
fm FontMetrics used to measure the String width
lastChar Character to get the right side bearing for.

public static int getUIDefaultsInt (Object key, Locale l)

Returns an integer from the defaults table that is appropriate for the given locale. If key does not map to a valid Integer, or can not be convered from a String to an integer, the value 0 is returned.

Parameters
key an Object specifying the int. Returned value is 0 if key is not available,
l the Locale for which the int is desired
Returns
  • the int

public static int getUIDefaultsInt (Object key, Locale l, int defaultValue)

Returns an integer from the defaults table that is appropriate for the given locale. If key does not map to a valid Integer, or can not be convered from a String to an integer, default is returned.

Parameters
key an Object specifying the int. Returned value is 0 if key is not available,
l the Locale for which the int is desired
defaultValue Returned value if key is not available, or is not an Integer
Returns
  • the int

public static int getUIDefaultsInt (Object key)

Returns an integer from the defaults table. If key does not map to a valid Integer, or can not be convered from a String to an integer, the value 0 is returned.

Parameters
key an Object specifying the int.
Returns
  • the int

public static int getUIDefaultsInt (Object key, int defaultValue)

Returns an integer from the defaults table. If key does not map to a valid Integer, or can not be convered from a String to an integer, default is returned.

Parameters
key an Object specifying the int. Returned value is 0 if key is not available,
defaultValue Returned value if key is not available, or is not an Integer
Returns
  • the int

public static final boolean isComplexLayout (char[] text, int start, int limit)

checks whether TextLayout is required to handle characters.

Parameters
text characters to be tested
start start
limit limit
Returns
  • true if TextLayout is required false if TextLayout is not required

public static boolean isLocalDisplay ()

public static SwingUtilities2.Section liesInHorizontal (Rectangle rect, Point p, boolean ltr, boolean 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.

See the documentation for #liesIn for more information on how the section is calculated.

Parameters
rect the rectangle
p the point the check
ltr true for left to right orientation, or false for right to left orientation
three true for three sections, or false for two
Returns
  • the Section where the point lies
Throws
NullPointerException if rect or p are null

public static SwingUtilities2.Section liesInVertical (Rectangle rect, Point p, boolean 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.

See the documentation for #liesIn for more information on how the section is calculated.

Parameters
rect the rectangle
p the point the check
three true for three sections, or false for two
Returns
  • the Section where the point lies
Throws
NullPointerException if rect or p are null

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. Otherwise, this method returns -1. This is used to make WindowsL&F JFileChooser act like native dialogs.

public static Object makeIcon (Class<?> baseClass, Class<?> rootClass, String imageFile)

Utility method that creates a UIDefaults.LazyValue that creates an ImageIcon UIResource for the specified image file name. The image is loaded using getResourceAsStream, starting with a call to that method on the base class parameter. If it cannot be found, searching will continue through the base class' inheritance hierarchy, up to and including rootClass.

Parameters
baseClass the first class to use in searching for the resource
rootClass an ancestor of baseClass to finish the search at
imageFile the name of the file to be found
Returns
  • a lazy value that creates the ImageIcon UIResource for the image, or null if it cannot be found

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. (Column must be 0). Does not check the "Table.isFileList" property. That should be checked before calling this method. This is used to make WindowsL&F JFileChooser act like native dialogs.

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. This property, used for text components acting as editors in a table or tree, tells DefaultCaret how many clicks to skip before starting selection.

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 c, FontMetrics fm, String string)

Returns the width of the passed in String. If the passed String is null, returns zero.

Parameters
c JComponent that will display the string, may be null
fm FontMetrics used to measure the String width
string String to get the width of

public static Future<V> submit (Callable<V> task)

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

Parameters
task the task to submit
Returns
  • a Future representing pending completion of the task
Throws
NullPointerException if the task is null

public static Future<V> submit (Runnable task, V result)

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

Parameters
task the task to submit
result the result to return upon successful completion
Returns
  • a Future representing pending completion of the task, and whose get() method will return the given result value upon completion
Throws
NullPointerException if the task is null

public static boolean tabbedPaneChangeFocusTo (Component comp)

Change focus to the visible component in JTabbedPane. This is not a general-purpose method and is here only to permit sharing code.

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

Determines whether the SelectedTextColor should be used for painting text foreground for the specified highlight. Returns true only if the highlight painter for the specified highlight is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color is null or equals to the selection color of the text component. This is a hack for fixing both bugs 4761990 and 5003294