Class JUtilities


  • public class JUtilities
    extends java.lang.Object
    General Swing utilities.
    Author:
    Chris Jennings
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int ALIGN_BOTTOM
      Vertical alignment constant.
      static int ALIGN_CENTER
      Horizontal alignment constant.
      static int ALIGN_LEFT
      Horizontal alignment constant.
      static int ALIGN_MIDDLE
      Vertical alignment constant.
      static int ALIGN_RIGHT
      Horizontal alignment constant.
      static int ALIGN_TOP
      Vertical alignment constant.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addDebugListeners​(java.io.PrintStream out, java.lang.Object component, boolean includeAllMoveEvents, boolean recursive)
      Adds listeners for all addXXXListener methods on a component that print information to a selected output stream.
      static boolean backButton​(java.awt.event.InputEvent e)
      Returns true if the "back" mouse button was held down during the given InputEvent and this button can be detected by this JVM.
      static javax.swing.border.Border createCompoundBorder​(javax.swing.border.Border... outerToInner)
      Returns a border that is the composition of any number of other borders.
      static void enable​(boolean enable, javax.swing.JComponent... components)
      Enable or disable a group list of components.
      static void enable​(boolean enable, javax.swing.JComponent component1, javax.swing.JComponent component2)  
      static void enableTree​(javax.swing.JComponent component, boolean enable)
      Recursively enable or disable a tree of components.
      static java.awt.Window findWindow​(java.awt.Component component)
      Returns the window that a component is contained within, if any.
      static boolean forwardButton​(java.awt.event.InputEvent e)
      Returns true if the "forward" mouse button was held down during the given InputEvent and this button can be detected by this JVM.
      static java.awt.Dimension getPreferredSizeForHTMLLabel​(java.lang.String html, boolean width, int prefSize)
      Determines the height (width) needed to display an HTML label with a specified width (height).
      static void hideWaitCursor​(java.awt.Component c)  
      static void installDisabledHTMLFix​(javax.swing.JComponent c)  
      static boolean isAcceleratorInUse​(javax.swing.JMenuBar bar, javax.swing.KeyStroke accel)
      Checks whether an accelerator key is already in use in a menu bar.
      static boolean isButtonDown​(int button, java.awt.event.InputEvent e)
      Returns true if the mouse button with the indicated number is down.
      static boolean leftButton​(java.awt.event.InputEvent e)
      Returns true if the left mouse button was held down during the given InputEvent.
      static boolean makeUtilityWindow​(java.awt.Window window)
      If supported by the JRE and the underlying platform, make a window a "utility" window.
      static boolean midButton​(java.awt.event.InputEvent e)
      Returns true if the middle mouse button was held down during the given InputEvent.
      static boolean rightButton​(java.awt.event.InputEvent e)
      Returns true if the right mouse button was held down during the given InputEvent.
      static void setIconPair​(javax.swing.AbstractButton button, java.lang.String standard, java.lang.String highlight, boolean rollover)
      Applies a pair of icons to a button.
      static void setIconPair​(javax.swing.AbstractButton button, javax.swing.Icon standard, javax.swing.Icon highlight, boolean rollover)
      Applies a pair of icons to a button.
      static void showWaitCursor​(java.awt.Component c)  
      static void snapToDesktop​(java.awt.Window window)
      Adjusts a window's location so that it does not extend beyond the edge of the display or overlap system areas (such as the task bar on Windows).
      static void snapToPoint​(java.awt.Window window, java.awt.Point point, int halign, int valign)
      Adjusts a window's location to align it with a point on the screen.
      static void snapToPointer​(java.awt.Window window)
      Adjusts a window's location so that the center is under the pointer.
      static void snapToPointer​(java.awt.Window window, int halign, int valign)
      Adjusts a window's location to align it with the pointing device's pointer on screen.
      static void threadAssert()
      Throws an assertion if the calling thread is not the event dispatch thread.
      static void uninstallDisabledHTMLFix​(javax.swing.JComponent c)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • ALIGN_LEFT

        public static int ALIGN_LEFT
        Horizontal alignment constant.
      • ALIGN_CENTER

        public static int ALIGN_CENTER
        Horizontal alignment constant.
      • ALIGN_RIGHT

        public static int ALIGN_RIGHT
        Horizontal alignment constant.
      • ALIGN_TOP

        public static int ALIGN_TOP
        Vertical alignment constant.
      • ALIGN_MIDDLE

        public static int ALIGN_MIDDLE
        Vertical alignment constant.
      • ALIGN_BOTTOM

        public static int ALIGN_BOTTOM
        Vertical alignment constant.
    • Method Detail

      • enable

        public static void enable​(boolean enable,
                                  javax.swing.JComponent... components)
        Enable or disable a group list of components.
      • enable

        public static void enable​(boolean enable,
                                  javax.swing.JComponent component1,
                                  javax.swing.JComponent component2)
      • enableTree

        public static void enableTree​(javax.swing.JComponent component,
                                      boolean enable)
        Recursively enable or disable a tree of components.
        Parameters:
        component - the root of the component tree
        enable - if true, enable the components, else disable
        Throws:
        java.lang.NullPointerException - if c is null
      • findWindow

        public static java.awt.Window findWindow​(java.awt.Component component)
        Returns the window that a component is contained within, if any.
        Parameters:
        component - the component to find the window of
        Returns:
        the containing window, or null
        Throws:
        java.lang.NullPointerException - if c is null
      • snapToPointer

        public static void snapToPointer​(java.awt.Window window)
        Adjusts a window's location so that the center is under the pointer. This is a cover for snapToPointer( window, ALIGN_CENTER, ALIGN_MIDDLE ).
        Parameters:
        window - the window to adjust
      • snapToPointer

        public static void snapToPointer​(java.awt.Window window,
                                         int halign,
                                         int valign)
        Adjusts a window's location to align it with the pointing device's pointer on screen. The window will be moved as close as possible to the pointer without allowing it to fall outside of the available desktop area any reserved system areas along the edges of that desktop. If there is no pointing device, or the pointer location cannot be determined, the window will be centered.
        Parameters:
        window - the window to adjust
        halign - a horizontal alignment value (ALIGN_LEFT, ALIGN_CENTER, or ALIGN_RIGHT)
        valign - a vertical alignment value (ALIGN_TOP, ALIGN_MIDDLE, or ALIGN_BOTTOM)
      • snapToPoint

        public static void snapToPoint​(java.awt.Window window,
                                       java.awt.Point point,
                                       int halign,
                                       int valign)
        Adjusts a window's location to align it with a point on the screen. The window will be moved as close as possible to the point without allowing it to fall outside of the available desktop area any reserved system areas along the edges of that desktop.
        Parameters:
        window - the window to adjust
        point - the point to align against, in screen coordinates
        halign - a horizontal alignment value (ALIGN_LEFT, ALIGN_CENTER, or ALIGN_RIGHT)
        valign - a vertical alignment value (ALIGN_TOP, ALIGN_MIDDLE, or ALIGN_BOTTOM)
      • snapToDesktop

        public static void snapToDesktop​(java.awt.Window window)
        Adjusts a window's location so that it does not extend beyond the edge of the display or overlap system areas (such as the task bar on Windows). If the window cannot fit completely in this area, then it will be resized to the available area.
        Parameters:
        window - the window to adjust
      • addDebugListeners

        public static void addDebugListeners​(java.io.PrintStream out,
                                             java.lang.Object component,
                                             boolean includeAllMoveEvents,
                                             boolean recursive)
        Adds listeners for all addXXXListener methods on a component that print information to a selected output stream.
        Parameters:
        out - the stream to print to (System.err if null)
        component - the component to add listeners to
        includeAllMoveEvents - if true, low-level cursor movements are included
        recursive - if true, listeners are added recursively to children
      • showWaitCursor

        public static void showWaitCursor​(java.awt.Component c)
      • hideWaitCursor

        public static void hideWaitCursor​(java.awt.Component c)
      • isButtonDown

        public static boolean isButtonDown​(int button,
                                           java.awt.event.InputEvent e)
        Returns true if the mouse button with the indicated number is down. If the button number is greater than the number of buttons recognized by this JVM, false is returned. Buttons from 1 to 3 can always be recognized; higher-numbered buttons may be recognized. The highest possible button number that might be recognized is MouseInfo.getNumberOfButtons(). Button numbers higher than this can be safely requested but will return false.
        Parameters:
        button - the number of the button (1 for BUTTON1, etc.) to test for
        e - the input event to test
        Returns:
        true if the button can be detected and is down
        Throws:
        java.lang.IllegalArgumentException - if the button number is less than 1
      • leftButton

        public static boolean leftButton​(java.awt.event.InputEvent e)
        Returns true if the left mouse button was held down during the given InputEvent.
        Parameters:
        e - the event to test
        Returns:
        true is mouse button was down
      • midButton

        public static boolean midButton​(java.awt.event.InputEvent e)
        Returns true if the middle mouse button was held down during the given InputEvent.
        Parameters:
        e - the event to test
        Returns:
        true is mouse button was down
      • rightButton

        public static boolean rightButton​(java.awt.event.InputEvent e)
        Returns true if the right mouse button was held down during the given InputEvent.
        Parameters:
        e - the event to test
        Returns:
        true is mouse button was down
      • backButton

        public static boolean backButton​(java.awt.event.InputEvent e)
        Returns true if the "back" mouse button was held down during the given InputEvent and this button can be detected by this JVM.
        Parameters:
        e - the event to test
        Returns:
        true is mouse button was down
        See Also:
        isButtonDown(int, java.awt.event.InputEvent)
      • forwardButton

        public static boolean forwardButton​(java.awt.event.InputEvent e)
        Returns true if the "forward" mouse button was held down during the given InputEvent and this button can be detected by this JVM.
        Parameters:
        e - the event to test
        Returns:
        true is mouse button was down
        See Also:
        isButtonDown(int, java.awt.event.InputEvent)
      • makeUtilityWindow

        public static boolean makeUtilityWindow​(java.awt.Window window)
        If supported by the JRE and the underlying platform, make a window a "utility" window. Utility windows are useful for features such as palettes and floating tool bars. When supported, they typically result in the window having a smaller title bar.
        Parameters:
        window - the window to make into a utility window
        Returns:
        true if the attempt succeeds, false otherwise
      • threadAssert

        public static void threadAssert()
        Throws an assertion if the calling thread is not the event dispatch thread.
        Throws:
        java.lang.AssertionError - if called from a thread other than the EDT
      • createCompoundBorder

        public static javax.swing.border.Border createCompoundBorder​(javax.swing.border.Border... outerToInner)
        Returns a border that is the composition of any number of other borders. Unlike BorderFactory.createCompoundBorder, this method can compose an arbitrary number of borders. In no case will null be returned; if no borders are passed in, an empty border will be returned. If one border is passed in, it is returned unchanged. If two borders are passed in, a simple compound border will be returned. Otherwise, a nested compound border that composes the borders is returned.
        Parameters:
        outerToInner - the borders to compose
        Returns:
        a border that composes all of the requested borders into a single border
      • installDisabledHTMLFix

        public static void installDisabledHTMLFix​(javax.swing.JComponent c)
      • uninstallDisabledHTMLFix

        public static void uninstallDisabledHTMLFix​(javax.swing.JComponent c)
      • getPreferredSizeForHTMLLabel

        public static java.awt.Dimension getPreferredSizeForHTMLLabel​(java.lang.String html,
                                                                      boolean width,
                                                                      int prefSize)
        Determines the height (width) needed to display an HTML label with a specified width (height).
        Parameters:
        html - the HTML string being displayed in a label
        prefSize - the preferred width or height of the component
        width - if true, the preferred size dimension is width; otherwise height
        Returns:
        the new preferred size of the component, with the specified dimension set to the preferred size and other dimension calculated
      • setIconPair

        public static void setIconPair​(javax.swing.AbstractButton button,
                                       javax.swing.Icon standard,
                                       javax.swing.Icon highlight,
                                       boolean rollover)
        Applies a pair of icons to a button. The standard icon is displayed normally; the highlight icon is displayed when the button is pressed. If rollover is enabled, the highlight icon is set for both rollover highlights and selection.
        Parameters:
        button - the button to modify
        standard - the normal icon for the button
        highlight - the highlight icon for the button
        rollover - if true, rollover effects are applied
      • setIconPair

        public static void setIconPair​(javax.swing.AbstractButton button,
                                       java.lang.String standard,
                                       java.lang.String highlight,
                                       boolean rollover)
        Applies a pair of icons to a button. This is equivalent to the icon version of this method, except that it takes the names of icon resources instead of the icons themselves.
        Parameters:
        button - the button to modify
        standard - the normal icon resource
        highlight - the highlight icon resource
        rollover - if true, rollover effects are applied
      • isAcceleratorInUse

        public static boolean isAcceleratorInUse​(javax.swing.JMenuBar bar,
                                                 javax.swing.KeyStroke accel)
        Checks whether an accelerator key is already in use in a menu bar.
        Parameters:
        bar - the menu bar to search
        accel - the accelerator key stroke to check for
        Returns:
        true if the accelerator is used by an item in the menu bar