Class UIUtilities

java.lang.Object
org.jjazz.uiutilities.api.UIUtilities

public class UIUtilities extends Object
  • Constructor Details

    • UIUtilities

      public UIUtilities()
  • Method Details

    • getFileChooserInstance

      public static JFileChooser getFileChooserInstance()
    • getEffectiveScreenArea

      public static Rectangle getEffectiveScreenArea(Component c)
      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

      public static Rectangle2D getStringBounds(String text, Font f)
      Get the size of a text with the specified font.

      Use a temporary BufferedImage() to calculate the sizing.

    • getUsableArea

      public static Rectangle getUsableArea(JComponent jc)
      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 component
      padding - The maximum space between dialog and anchor component.
      horizontalPercent - 0 means left of anchor component, 1 is right, 0.5 is center
      verticalPercent - 0 means above of anchor component, 1 is below, 0.5 is center
    • getAction

      public static Action getAction(ActionListener al)
      Create an AbstractAction from the specified ActionListener.
      Parameters:
      al -
      Returns:
    • getJListModelAsList

      public static <T> List<T> getJListModelAsList(DefaultListModel<T> listModel)
      Convert a JList default list model into a normal list.
      Type Parameters:
      T -
      Parameters:
      listModel -
      Returns:
    • installEscapeKeyAction

      public static void installEscapeKeyAction(JDialog dialog, Runnable r)
      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

      public static void installEnterKeyAction(JDialog dialog, Runnable r)
      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

      public static void invokeLaterIfNeeded(Runnable run)
      If already on the EDT, call run.run(), otherwise use SwingUtilities.invokeLater(run).
      Parameters:
      run -
    • actionToMenuItems

      public static List<JComponent> actionToMenuItems(Action action, org.openide.util.Lookup context)
      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

      public static void setRecursiveEnabled(boolean b, JComponent jc)
      Recursively enable/disable a JComponent and its JComponent children.
      Parameters:
      b - boolean
      jc - JComponent
    • calculateInverseColor

      public static Color calculateInverseColor(Color c)
    • installSelectAllWhenFocused

      public static void installSelectAllWhenFocused(JTextComponent comp)
      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

      public static void installPrintableAsciiKeyTrap(JTextComponent textComponent)
      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

      public static KeyStroke getGenericControlKeyStroke(int keyEventCode)
      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

      public static KeyStroke getGenericControlShiftKeyStroke(int keyEventCode)
      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

      public static KeyStroke getGenericAltKeyStroke(int keyEventCode)
      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

      public static Color calculateDisabledColor(Color c)
    • 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

      public static void changeMenuFontSize(JMenu menu, float fontSizeOffset)
      Change the font size of a menu and of its components (which can be submenus).
      Parameters:
      menu -
      fontSizeOffset -
    • changeFontSize

      public static void changeFontSize(Component c, float fontSizeOffset)
      Change the font size of a component.
      Parameters:
      c -
      fontSizeOffset - eg -2 (smaller) or +1.5 (bigger)
    • showDirChooser

      public static File showDirChooser(String dirPath, String title)
      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

      public static void drawStringAligned(Graphics2D g2, JComponent jc, String text, int hAlign)
      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

      public static void disableContainer(Container container)
      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

      public static void enableContainer(Container container)
      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 below container in the component hierarchy and return nested components that are instances of class clazz 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 search
      nested - true to list components nested within another listed component, false otherwise
      Returns:
      the List of components
    • buildSlider

      public static JSlider buildSlider(int orientation, float ratio)
      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

      public static void addChangeListener(JTextComponent text, ChangeListener changeListener)
      Installs a listener to receive notification when the text of any JTextComponent is changed.

      Internally, it installs a DocumentListener on the text component's Document, and a PropertyChangeListener on the text component to detect if the Document 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 a JTextField or JTextArea
      changeListener - a listener to receieve ChangeEvents when the text is changed; the source object for the events will be the text component
      Throws:
      NullPointerException - if either parameter is null