Class UIUtilities
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic List<JComponent>
actionToMenuItems
(Action action, org.openide.util.Lookup context) Create one or more JMenuItems or JSeparators from a Netbeans action.static void
addChangeListener
(JTextComponent text, ChangeListener changeListener) Installs a listener to receive notification when the text of anyJTextComponent
is changed.static JSlider
buildSlider
(int orientation, float ratio) Build a JSlider with an adjusted preferred length.static Color
static Color
static void
changeFontSize
(Component c, float fontSizeOffset) Change the font size of a component.static void
changeMenuBarFontSize
(org.openide.awt.MenuBar menuBar, float fontSizeOffset) Change the font size of a menuBar and its submenus.static void
changeMenuFontSize
(JMenu menu, float fontSizeOffset) Change the font size of a menu and of its components (which can be submenus).static JLayer<JComponent>
createEnterExitComponentLayer
(JComponent comp, Consumer<Boolean> enterExitListener) Creates a JLayer for comp so that enterExitListener is *reliably* called when mouse enters/exit comp.static void
disableContainer
(Container container) Convenience static method to disable all components of a given Container, including nested Containers.static void
drawStringAligned
(Graphics2D g2, JComponent jc, String text, int hAlign) Draw a string left-aligned/centered/right-aligned on component jc.static void
enableContainer
(Container container) Convenience static method to enable Components previously disabled by using the disableContainer() method.static Action
Create an AbstractAction from the specified ActionListener.static <T extends JComponent>
List<T>getDescendantsOfType
(Class<T> clazz, Container container, boolean nested) Convenience method for searching belowcontainer
in the component hierarchy and return nested components that are instances of classclazz
it finds.static Rectangle
Get the current screen bounds where specified component is displayed, excluding possible taskbars.static JFileChooser
static KeyStroke
getGenericAltKeyStroke
(int keyEventCode) Get a control-shift key KeyStroke which works on all OSes: Win, Linux AND Mac OSX.static KeyStroke
getGenericControlKeyStroke
(int keyEventCode) Get a control-key KeyStroke which works on all OSes: Win, Linux AND Mac OSX.static KeyStroke
getGenericControlShiftKeyStroke
(int keyEventCode) Get a control-sfhit key KeyStroke which works on all OSes: Win, Linux AND Mac OSX.static <T> List<T>
getJListModelAsList
(DefaultListModel<T> listModel) Convert a JList default list model into a normal list.static Rectangle2D
getStringBounds
(String text, Font f) Get the size of a text with the specified font.static Rectangle
Gets the usable rectangle area within the borders (insets) of the JComponent.static void
installEnterKeyAction
(JDialog dialog, Runnable r) Install an action on a dialog when the ENTER key is pressed.static void
installEscapeKeyAction
(JDialog dialog, Runnable r) Install an action on a dialog when the ESCAPE key is pressed.static void
installPrintableAsciiKeyTrap
(JTextComponent textComponent) Make the specified textComponent capture all ASCII printable key presses.static void
Install a listener to automatically select all text when component gets the focus.static void
If already on the EDT, call run.run(), otherwise use SwingUtilities.invokeLater(run).static void
setDialogLocationRelativeTo
(Dialog dialog, Component anchorComponent, int padding, double horizontalPercent, double verticalPercent) Positions a dialog at a position relative to an anchor component.static void
setRecursiveEnabled
(boolean b, JComponent jc) Recursively enable/disable a JComponent and its JComponent children.static File
showDirChooser
(String dirPath, String title) Show the JFileChooser to select a directory.
-
Constructor Details
-
UIUtilities
public UIUtilities()
-
-
Method Details
-
getFileChooserInstance
-
getEffectiveScreenArea
Get the current screen bounds where specified component is displayed, excluding possible taskbars.Supposed to handle correctly multiple monitors on various OS.
See https://stackoverflow.com/questions/10123735/get-effective-screen-size-from-java/10123912 (answer of Rasmus Faber)
- Parameters:
c
-- Returns:
-
getStringBounds
Get the size of a text with the specified font.Use a temporary BufferedImage() to calculate the sizing.
-
getUsableArea
Gets the usable rectangle area within the borders (insets) of the JComponent.- Parameters:
jc
-- Returns:
-
setDialogLocationRelativeTo
public static void setDialogLocationRelativeTo(Dialog dialog, Component anchorComponent, int padding, double horizontalPercent, double verticalPercent) Positions a dialog at a position relative to an anchor component.- Parameters:
dialog
- the dialog to be positioned.anchorComponent
- the anchor componentpadding
- The maximum space between dialog and anchor component.horizontalPercent
- 0 means left of anchor component, 1 is right, 0.5 is centerverticalPercent
- 0 means above of anchor component, 1 is below, 0.5 is center
-
getAction
Create an AbstractAction from the specified ActionListener.- Parameters:
al
-- Returns:
-
getJListModelAsList
Convert a JList default list model into a normal list.- Type Parameters:
T
-- Parameters:
listModel
-- Returns:
-
installEscapeKeyAction
Install an action on a dialog when the ESCAPE key is pressed.- Parameters:
dialog
-r
- Call r.run() when ESCAPE is pressed. If r is null pressing ESCAPE closes the dialog.
-
installEnterKeyAction
Install an action on a dialog when the ENTER key is pressed.- Parameters:
dialog
-r
- Call r.run() when ENTER is pressed. If r is null pressing ENTER closes the dialog.
-
invokeLaterIfNeeded
If already on the EDT, call run.run(), otherwise use SwingUtilities.invokeLater(run).- Parameters:
run
-
-
actionToMenuItems
Create one or more JMenuItems or JSeparators from a Netbeans action.Copied from part of org.openide.util.Utilities.actionsToPopup(). Special handling if action is instance of:
- ContextAwareAction
- Presenter.Popup
If Presenter.Popup is implemented and the JMenuItem returned by getPopupPresenter()... :
- has client property DynamicMenuContent.HIDE_WHEN_DISABLED, then no menu item is created if action is disabled.
- is instance of DynamicContent, then use the result of item.getMenuPresenters() (JMenuItems, or JSeparators for null values).- Parameters:
action
-context
- The context used for the action if it's a ContextAwareAction instance- Returns:
- A list of JMenuItems or JSeparators. Can be empty.
-
setRecursiveEnabled
Recursively enable/disable a JComponent and its JComponent children.- Parameters:
b
- booleanjc
- JComponent
-
calculateInverseColor
-
installSelectAllWhenFocused
Install a listener to automatically select all text when component gets the focus.- Parameters:
comp
-
-
createEnterExitComponentLayer
public static JLayer<JComponent> createEnterExitComponentLayer(JComponent comp, Consumer<Boolean> enterExitListener) Creates a JLayer for comp so that enterExitListener is *reliably* called when mouse enters/exit comp.- Parameters:
comp
-enterExitListener
- Called whenever mouse enters (bool=true) or exits comp's bounds.- Returns:
-
installPrintableAsciiKeyTrap
Make the specified textComponent capture all ASCII printable key presses.Key presses are used by an editable JTextComponent to display the chars, but it does not consume the key presses. So they are transmitted up the containment hierarchy via the keybinding framework. This means a global Netbeans action might be triggered if user types a global action shortcut (eg SPACE) in the JTextComponent.
This method makes textComponent capture all ASCII printable key presses (ASCII char from 32 to 126) to avoid this behaviour.
- Parameters:
textComponent
-
-
getGenericControlKeyStroke
Get a control-key KeyStroke which works on all OSes: Win, Linux AND Mac OSX.- Parameters:
keyEventCode
- A KeyEvent constant like KeyEvent.VK_M (for ctrl-M)- Returns:
-
getGenericControlShiftKeyStroke
Get a control-sfhit key KeyStroke which works on all OSes: Win, Linux AND Mac OSX.- Parameters:
keyEventCode
- A KeyEvent constant like KeyEvent.VK_M (for ctrl-M)- Returns:
-
getGenericAltKeyStroke
Get a control-shift key KeyStroke which works on all OSes: Win, Linux AND Mac OSX.- Parameters:
keyEventCode
- A KeyEvent constant like KeyEvent.VK_M (for ctrl-shift-M)- Returns:
-
calculateDisabledColor
-
changeMenuBarFontSize
public static void changeMenuBarFontSize(org.openide.awt.MenuBar menuBar, float fontSizeOffset) Change the font size of a menuBar and its submenus.- Parameters:
menuBar
-fontSizeOffset
- eg -2 (smaller) or +1.5 (bigger)
-
changeMenuFontSize
Change the font size of a menu and of its components (which can be submenus).- Parameters:
menu
-fontSizeOffset
-
-
changeFontSize
Change the font size of a component.- Parameters:
c
-fontSizeOffset
- eg -2 (smaller) or +1.5 (bigger)
-
showDirChooser
Show the JFileChooser to select a directory.- Parameters:
dirPath
- Initialize chooser with this directory.title
- Title of the dialog.- Returns:
- The selected dir or null.
-
drawStringAligned
Draw a string left-aligned/centered/right-aligned on component jc.If component width is too small for text to be centered, text is aligned on the left.
If string contains '\n', string will be displayed on several lines.
- Parameters:
g2
- Used to draw the string with the default font and color.jc
-text
-hAlign
- 0=left, 1=centered, 2=right
-
disableContainer
Convenience static method to disable all components of a given Container, including nested Containers.The method saves the enabled state of children, in order to reenable them (or not) as required when calling enableContainer().
- Parameters:
container
- the Container containing Components to be disabled- See Also:
-
enableContainer
Convenience static method to enable Components previously disabled by using the disableContainer() method.Only Components disable by the disableContainer() method will be enabled.
- Parameters:
container
- a Container that has been previously disabled.- See Also:
-
getDescendantsOfType
public static <T extends JComponent> List<T> getDescendantsOfType(Class<T> clazz, Container container, boolean nested) Convenience method for searching belowcontainer
in the component hierarchy and return nested components that are instances of classclazz
it finds.Returns an empty list if no such components exist in the container.
Invoking this method with a class parameter of JComponent.class will return all nested components.
- Type Parameters:
T
-- Parameters:
clazz
- the class of components whose instances are to be found.container
- the container at which to begin the searchnested
- true to list components nested within another listed component, false otherwise- Returns:
- the List of components
-
buildSlider
Build a JSlider with an adjusted preferred length.- Parameters:
orientation
- The orientation of the JSlider, SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.ratio
- The ratio to be applied on the preferred length (width or height, depending on orientation).- Returns:
-
addChangeListener
Installs a listener to receive notification when the text of anyJTextComponent
is changed.Internally, it installs a
DocumentListener
on the text component'sDocument
, and aPropertyChangeListener
on the text component to detect if theDocument
itself is replaced.Usage: addChangeListener(someTextBox, e -> doSomething());
From Stackoverflow: https://stackoverflow.com/questions/3953208/value-change-listener-to-jtextfield
- Parameters:
text
- any text component, such as aJTextField
orJTextArea
changeListener
- a listener to receieveChangeEvent
s when the text is changed; the source object for the events will be the text component- Throws:
NullPointerException
- if either parameter is null
-