public final class SwingComponentUtils extends Object
Modifier and Type | Field and Description |
---|---|
static int |
LEFT_FACING |
static int |
RIGHT_FACING |
Modifier and Type | Method and Description |
---|---|
static boolean |
askConfirmation(Window aWindow,
String aMessage)
Asks the user for confirmation.
|
static boolean |
askConfirmation(Window aWindow,
String aMessage,
String aTitle)
Asks the user for confirmation.
|
static JComponent |
createButtonPane(JButton... aButtons)
Creates a button pane in which the given buttons are neatly aligned with
proper spacings.
|
static JComboBox |
createChannelSelector(int aChannelCount)
Creates a channel selector combobox, where only a valid channel can be
selected.
|
static JComboBox |
createChannelSelector(int aChannelCount,
int aDefaultSelectedIndex)
Creates a channel selector combobox, where only a valid channel can be
selected.
|
static KeyStroke |
createKeyMask(int aKeyStroke,
int... aMasks)
Convenience method to create a key mask.
|
static KeyStroke |
createMenuKeyMask(int aKeyStroke,
int... aMasks)
Convenience method to create a key mask for menu's.
|
static JComboBox |
createOptionalChannelSelector(int aChannelCount)
Creates a channel selector combobox, where optionally a channel can be
selected.
|
static JComboBox |
createOptionalChannelSelector(int aChannelCount,
int aDefaultSelectedIndex)
Creates a channel selector combobox, where optionally a channel can be
selected.
|
static JLabel |
createRightAlignedLabel(String aText)
Creates a JLabel which text is right aligned.
|
static void |
dispose(Window aWindow)
Closes and disposes a given
Window . |
static void |
drawArrowHead(Graphics2D aG,
int aXpos,
int aYpos,
int aFactor,
int aArrowWidth,
int aArrowHeight)
Draws a single arrow head
|
static void |
drawDoubleHeadedArrow(Graphics aG,
int aX1,
int aY,
int aX2)
Draws a double headed arrow of 8x8.
|
static void |
drawDoubleHeadedArrow(Graphics aG,
int aX1,
int aY1,
int aX2,
int aY2)
Draws a double headed arrow of 8x8.
|
static void |
drawDoubleHeadedArrow(Graphics aG,
int aX1,
int aY1,
int aX2,
int aY2,
int aArrowWidth,
int aArrowHeight)
Draws a double headed arrow with arrow heads of a given width and height.
|
static void |
fixLabelWidth(JLabel aLabel,
String aMinimalText)
"Fixates" the preferred width of the given label to the given text.
|
static <T> T |
getAncestorOfClass(Class<T> aType,
Component aComponent)
Convenience method for searching above the given component in the component
hierarchy and returns the first object of the given type it finds, or
null if no such parent was found. |
static Window |
getCurrentWindow()
Tries to find the current focused window.
|
static JComponent |
getDeepestComponentAt(Component aParent,
int aXpos,
int aYpos)
Returns the deepest visible descendent Component of
parent
that contains the location x , y . |
static JComponent |
getDeepestComponentAt(MouseEvent aEvent)
Returns the deepest visible descendent Component of
parent
that contains the location x , y . |
static int |
getMenuShortcutKeyMask()
Returns the key mask of the menu shortcut key.
|
static Window |
getOwningWindow(AWTEvent aEvent)
Tries to find the owning window for the AWT-event's source.
|
static Window |
getOwningWindow(Component aComponent)
Tries to find the owning window for the given component.
|
static int |
getStringWidth(Font aFont,
String aString)
Returns the string width for a given
Font and string. |
static int |
getStringWidth(String aString)
Returns the string width for the default label font and string.
|
static void |
invokeOnEDT(Runnable aRunnable)
Similar as to
SwingUtilities.invokeLater(Runnable) , but does a
check first if the current running thread is already the EDT. |
static boolean |
isActivelyShown(Component aComponent)
Returns whether the given component is "actively" shown in screen, that is,
it or any of its ancestors is focused.
|
static void |
loadWindowState(org.osgi.service.prefs.Preferences aProperties,
Window aWindow)
Tries to load/restore the window state of the given window.
|
static void |
registerKeyBinding(JComponent aComponent,
char aKey,
Action aAction)
Registers a given keystroke to invoke a given action on the given
component.
|
static void |
registerKeyBinding(JComponent aComponent,
KeyStroke aKeyStroke,
Action aAction)
Registers a given keystroke to invoke a given action on the given
component.
|
static void |
registerKeyBinding(JComponent aComponent,
String aKeyStroke,
Action aAction)
Registers a given keystroke to invoke a given action on the given
component.
|
static void |
registerKeystroke(JComponent aComponent,
Action aAction,
String aCommandName)
Registers the keystroke of the given action as "command" of the given
component.
|
static void |
saveWindowState(org.osgi.service.prefs.Preferences aProperties,
Window aWindow)
Saves the window state to the given properties map.
|
static void |
setupWindowContentPane(Window aDialog,
Component aCenterComponent,
Component aButtonPane)
Sets up the given dialog's content pane by setting its border to provide a
good default spacer, and setting the content pane to the given components.
|
static void |
setupWindowContentPane(Window aWindow,
Component aCenterComponent,
Component aButtonPane,
JButton defaultButton)
Sets up the given window content pane by setting its border to provide a
good default spacer, and setting the content pane to the given components.
|
static File |
showFileOpenDialog(Window aOwner,
FileFilter... aFileFilters)
Shows a file-open selection dialog for the current working directory.
|
static File |
showFileOpenDialog(Window aOwner,
String aCurrentDirectory,
FileFilter... aFileFilters)
Shows a file-open selection dialog for the given working directory.
|
static File |
showFileSaveDialog(Window aOwner,
FileFilter... aFileFilters)
Shows a file-save selection dialog for the current working directory.
|
static File |
showFileSaveDialog(Window aOwner,
String aCurrentDirectory,
FileFilter... aFileFilters)
Shows a file-save selection dialog for the given working directory.
|
static File |
showFileSelectionDialog(Window aOwner,
FileFilter... aFileFilters)
Shows a file selection dialog for the current working directory.
|
static File |
showFileSelectionDialog(Window aOwner,
String aCurrentDirectory,
FileFilter... aFileFilters)
Shows a file selection dialog for the given working directory.
|
static String |
toCssString(Font aFont)
Converts a given font to a font-clause that can be used in a CSS-file.
|
public static final int LEFT_FACING
public static final int RIGHT_FACING
public static boolean askConfirmation(Window aWindow, String aMessage)
aWindow
- the parent window of the confirmation dialog;aMessage
- the message to display in the confirmation dialog.true
if the user acknowledged the confirmation,
false
otherwise.public static boolean askConfirmation(Window aWindow, String aMessage, String aTitle)
aWindow
- the parent window of the confirmation dialog;aMessage
- the message to display in the confirmation dialog;aTitle
- the title to display in the confirmation dialog.true
if the user acknowledged the confirmation,
false
otherwise.public static JComponent createButtonPane(JButton... aButtons)
aButtons
- the buttons to add to the created button pane, will be added in
the given order.null
.public static JComboBox createChannelSelector(int aChannelCount)
aChannelCount
- the number of channels to include in the combobox options;public static JComboBox createChannelSelector(int aChannelCount, int aDefaultSelectedIndex)
aChannelCount
- the number of channels to include in the combobox options;aDefaultSelectedIndex
- the default selected index for the created combobox.public static final KeyStroke createKeyMask(int aKeyStroke, int... aMasks)
aKeyStroke
- the key stroke to create a key mask for;aMasks
- the (optional) mask modifiers to use.public static final KeyStroke createMenuKeyMask(int aKeyStroke, int... aMasks)
aKeyStroke
- the key stroke to create a menu key mask for;aMasks
- the (optional) mask modifiers to use.public static JComboBox createOptionalChannelSelector(int aChannelCount)
aChannelCount
- the number of channels to include in the combobox options.public static JComboBox createOptionalChannelSelector(int aChannelCount, int aDefaultSelectedIndex)
aChannelCount
- the number of channels to include in the combobox options;aDefaultSelectedIndex
- the default selected index for the created combobox.public static final JLabel createRightAlignedLabel(String aText)
aText
- the text of the JLabel to create, may be null
.null
.public static void dispose(Window aWindow)
Window
.aWindow
- the window to close, if null
, this method doesn't do
anything.public static final void drawArrowHead(Graphics2D aG, int aXpos, int aYpos, int aFactor, int aArrowWidth, int aArrowHeight)
aG
- the canvas to draw on;aXpos
- the X position of the arrow head;aYpos
- the (center) Y position of the arrow head;aFactor
- +1 to have a left-facing arrow head, -1 to have a right-facing
arrow head;aArrowWidth
- the total width of the arrow head;aArrowHeight
- the total height of the arrow head.public static final void drawDoubleHeadedArrow(Graphics aG, int aX1, int aY, int aX2)
aG
- the canvas to draw on;aX1
- the starting X position of the arrow;aY
- the starting Y position of the arrow;aX2
- the ending X position of the arrow.public static final void drawDoubleHeadedArrow(Graphics aG, int aX1, int aY1, int aX2, int aY2)
aG
- the canvas to draw on;aX1
- the starting X position of the arrow;aY1
- the starting Y position of the arrow;aX2
- the ending X position of the arrow;aY2
- the ending Y position of the arrow.public static final void drawDoubleHeadedArrow(Graphics aG, int aX1, int aY1, int aX2, int aY2, int aArrowWidth, int aArrowHeight)
aG
- the canvas to draw on;aX1
- the starting X position of the arrow;aY1
- the starting Y position of the arrow;aX2
- the ending X position of the arrow;aY2
- the ending Y position of the arrow;aArrowWidth
- the total width of the arrow head;aArrowHeight
- the total height of the arrow head.public static final void fixLabelWidth(JLabel aLabel, String aMinimalText)
aLabel
- the label to fixate, cannot be null
;aMinimalText
- the text to use as minimal width indicator.public static <T> T getAncestorOfClass(Class<T> aType, Component aComponent)
null
if no such parent was found.
The reason this method exists is for tidyness of the calling code, as no longer a explicit cast is needed.
aType
- the type of the parent to find, cannot be null
;aComponent
- the component to search in the hierarchy, cannot be
null
.null
if not found.SwingUtilities.getAncestorOfClass(Class, Component)
public static final Window getCurrentWindow()
null
if no such window
could be found.public static JComponent getDeepestComponentAt(Component aParent, int aXpos, int aYpos)
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.aParent
- the root component to begin the searchaXpos
- the x target locationaYpos
- the y target locationpublic static JComponent getDeepestComponentAt(MouseEvent aEvent)
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.aParent
- the root component to begin the searchaXpos
- the x target locationaYpos
- the y target locationpublic static final int getMenuShortcutKeyMask()
public static final Window getOwningWindow(AWTEvent aEvent)
aEvent
- the AWT event to find the owning window for, may be
null
.null
if no such window could be
found, or a null
event was given.public static final Window getOwningWindow(Component aComponent)
aComponent
- the AWT event to find the owning window for, may be
null
.null
if no such window could be
found, or a null
component was given.public static int getStringWidth(Font aFont, String aString)
Font
and string.aFont
- the font to create the string width;aString
- the string to get the width for.public static int getStringWidth(String aString)
aString
- the string to get the width for.public static void invokeOnEDT(Runnable aRunnable)
SwingUtilities.invokeLater(Runnable)
, but does a
check first if the current running thread is already the EDT. If so, it
will directly call the Runnable.run()
method, otherwise leave it up
to SwingUtilities.invokeLater(Runnable)
to invoke it on a later
moment.aRunnable
- the runnable to call on the EDT, cannot be null
.public static final boolean isActivelyShown(Component aComponent)
aComponent
- the component to determine whether it is actively shown on screen,
may be null
.true
if the given component is actively shown,
false
otherwise.public static void loadWindowState(org.osgi.service.prefs.Preferences aProperties, Window aWindow)
aProperties
- the properties to read from;aWindow
- the window to load the state for.public static void registerKeyBinding(JComponent aComponent, char aKey, Action aAction)
aComponent
- the component to register the keystroke for;aKeyStroke
- the keystroke (as plain char) to register;aAction
- the action to invoke when the keystroke is typed.public static void registerKeyBinding(JComponent aComponent, KeyStroke aKeyStroke, Action aAction)
aComponent
- the component to register the keystroke for;aKeyStroke
- the keystroke to register;aAction
- the action to invoke when the keystroke is typed.public static void registerKeyBinding(JComponent aComponent, String aKeyStroke, Action aAction)
aComponent
- the component to register the keystroke for;aKeyStroke
- the keystroke (as String) to register;aAction
- the action to invoke when the keystroke is typed.public static void registerKeystroke(JComponent aComponent, Action aAction, String aCommandName)
This code is based on the Sulky-tools, found at <http://github.com/huxi/sulky>.
aComponent
- the component that should react on the keystroke, cannot be
null
;aAction
- the action of the keystroke, cannot be null
;aCommandName
- the name of the command to register the keystore under.public static void saveWindowState(org.osgi.service.prefs.Preferences aProperties, Window aWindow)
aProperties
- the properties to fill;aWindow
- the window to save the state for.public static void setupWindowContentPane(Window aDialog, Component aCenterComponent, Component aButtonPane)
aDialog
- the dialog to setup, cannot be null
;aCenterComponent
- the component that should appear at the center of the dialog;aButtonPane
- the component that should appear at the bottom of the dialog
(typically the buttons).public static void setupWindowContentPane(Window aWindow, Component aCenterComponent, Component aButtonPane, JButton defaultButton)
aWindow
- the window to setup, cannot be null
;aCenterComponent
- the component that should appear at the center of the dialog;aButtonPane
- the component that should appear at the bottom of the dialogdefaultButton
- the default button for this dialog; can be null for "none".JRootPane.setDefaultButton(javax.swing.JButton)
public static final File showFileOpenDialog(Window aOwner, FileFilter... aFileFilters)
aOwner
- the owning window to show the dialog in.null
if the user aborted the
dialog.public static final File showFileOpenDialog(Window aOwner, String aCurrentDirectory, FileFilter... aFileFilters)
aOwner
- the owning window to show the dialog in;aCurrentDirectory
- the working directory to start the dialog in, can be
null
.null
if the user aborted the
dialog.public static final File showFileSaveDialog(Window aOwner, FileFilter... aFileFilters)
aOwner
- the owning window to show the dialog in.null
if the user aborted the
dialog.public static final File showFileSaveDialog(Window aOwner, String aCurrentDirectory, FileFilter... aFileFilters)
aOwner
- the owning window to show the dialog in;aCurrentDirectory
- the working directory to start the dialog in, can be
null
.null
if the user aborted the
dialog.public static final File showFileSelectionDialog(Window aOwner, FileFilter... aFileFilters)
aOwner
- the owning window to show the dialog in.null
if the user aborted the
dialog.public static final File showFileSelectionDialog(Window aOwner, String aCurrentDirectory, FileFilter... aFileFilters)
aOwner
- the owning window to show the dialog in;aCurrentDirectory
- the working directory to start the dialog in, can be
null
.null
if the user aborted the
dialog.public static String toCssString(Font aFont)
aFont
- the font convert to CSS, cannot be null
.null
.IllegalArgumentException
- in case the given font was null
.Copyright © 2015 L'Xtreme IT consultancy. All rights reserved.