Package de.sciss.gui

Class GUIUtil


  • public class GUIUtil
    extends java.lang.Object
    This is a helper class containing utility static functions for common Swing / GUI operations
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void constrainHeight​(javax.swing.JComponent c, int h)
      Adjusts minimum, maximum and preferred size of a component so as to constrain its height to a given value.
      static void constrainSize​(javax.swing.JComponent c, int w, int h)
      Sets minimum, maximum and preferred size to given values.
      static void constrainWidth​(javax.swing.JComponent c, int w)
      Adjusts minimum, maximum and preferred size of a component so as to constrain its width to a given value.
      static java.awt.Point convertPoint​(java.awt.Component source, java.awt.Point aPoint, java.awt.Component destination)
      Same as SwingUtilities.convertPoint, but handles JViewports properly
      static void convertPointFromScreen​(java.awt.Point p, java.awt.Component c)
      Same as SwingUtilities.convertPointFromScreen, but handles JViewports properly
      static void convertPointToScreen​(java.awt.Point p, java.awt.Component c)
      Same as SwingUtilities.convertPointToScreen, but handles JViewports properly
      static java.awt.Rectangle convertRectangle​(java.awt.Component source, java.awt.Rectangle r, java.awt.Component destination)  
      static GradientPanel createGradientPanel()  
      static void createKeyAction​(javax.swing.AbstractButton comp, javax.swing.KeyStroke stroke)
      Convenience method that will add new corresponding entries in a button's input and action map, such that a given KeyStroke will cause a DoClickAction to be performed on that button.
      static void displayError​(java.awt.Component component, java.lang.Exception exception, java.lang.String title)
      Displays an error message dialog by examining a given Exception.
      static javax.swing.Icon getNoWriteIcon()
      Returns an Icon for a no-write or write-protection indicator.
      static java.lang.String getResourceString​(java.lang.String key)  
      static java.util.prefs.Preferences getUserPrefs()  
      static double getVersion()  
      static boolean isAlwaysOnTop​(java.awt.Component c)  
      static void makeCompactSpringGrid​(java.awt.Container parent, int rows, int cols, int initialX, int initialY, int xPad, int yPad)
      Aligns the first rows * cols components of parent in a grid.
      static void makeSpringGrid​(java.awt.Container parent, int rows, int cols, int initialX, int initialY, int xPad, int yPad)
      Aligns the first rows * cols components of parent in a grid.
      static void printSizes​(java.awt.Component c)
      A debugging utility that prints to stdout the component's minimum, preferred, and maximum sizes.
      static void removeMenuModifierBindings​(javax.swing.JComponent c, MenuGroup mg)
      Removes from a component's inputmap all bindings which may conflict with menu accelerators, that is those whose modifiers are equal to the secondary menu modifier, defined as control on mac and control+alt on windows and linux.
      static boolean setAlwaysOnTop​(java.awt.Component c, boolean b)  
      static void setDeepFont​(java.awt.Container c, java.awt.Font fnt)
      Set a font for a container and all children we can find in this container (calling this method recursively).
      static void setInitialDialogFocus​(javax.swing.JComponent c)
      Passes keyboard focus to a given component when that component is to be presented in a dialog.
      static void setPreferences​(java.awt.Container c, java.util.prefs.Preferences prefs)  
      static void wrapWindowBounds​(java.awt.Rectangle wr, java.awt.Rectangle sr)  
      • Methods inherited from class java.lang.Object

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

      • getUserPrefs

        public static final java.util.prefs.Preferences getUserPrefs()
      • getVersion

        public static final double getVersion()
      • getResourceString

        public static final java.lang.String getResourceString​(java.lang.String key)
      • displayError

        public static void displayError​(java.awt.Component component,
                                        java.lang.Exception exception,
                                        java.lang.String title)
        Displays an error message dialog by examining a given Exception. Returns after the dialog was closed by the user.
        Parameters:
        component - the component in which to open the dialog. null is allowed in which case the dialog will appear centered on the screen.
        exception - the exception that was thrown. the message's text is displayed using the getLocalizedMessage method.
        title - name of the action in which the error occurred
        See Also:
        JOptionPane.showOptionDialog( Component, Object, String, int, int, Icon, Object[], Object ), Throwable.getLocalizedMessage()
      • createKeyAction

        public static void createKeyAction​(javax.swing.AbstractButton comp,
                                           javax.swing.KeyStroke stroke)
        Convenience method that will add new corresponding entries in a button's input and action map, such that a given KeyStroke will cause a DoClickAction to be performed on that button. The key stroke is performed whenever the button is in the current focused window.
        Parameters:
        comp - an AbstractButton to which a a new keyboard action is attached.
        stroke - the KeyStroke which causes a click on the button.
        See Also:
        DoClickAction, JComponent.getInputMap( int ), JComponent.getActionMap(), JComponent.WHEN_IN_FOCUSED_WINDOW
      • setDeepFont

        public static void setDeepFont​(java.awt.Container c,
                                       java.awt.Font fnt)
        Set a font for a container and all children we can find in this container (calling this method recursively). This is necessary because calling setFont on a JPanel does not cause the Font of the gadgets contained in the panel to change their fonts.
        Parameters:
        c - the container to traverse for children whose font is to be changed
        fnt - the new font to apply; if null the current application's window handler's default font is used
        See Also:
        Component.setFont( Font )
      • setPreferences

        public static void setPreferences​(java.awt.Container c,
                                          java.util.prefs.Preferences prefs)
      • printSizes

        public static void printSizes​(java.awt.Component c)
        A debugging utility that prints to stdout the component's minimum, preferred, and maximum sizes. This is taken from the Sun Swing Tutorial Site.
      • makeSpringGrid

        public static void makeSpringGrid​(java.awt.Container parent,
                                          int rows,
                                          int cols,
                                          int initialX,
                                          int initialY,
                                          int xPad,
                                          int yPad)
        Aligns the first rows * cols components of parent in a grid. Each component is as big as the maximum preferred width and height of the components. The parent is made just big enough to fit them all.

        The code is taken from the Sun Swing Tutorial Site.

        Parameters:
        rows - number of rows
        cols - number of columns
        initialX - x location to start the grid at
        initialY - y location to start the grid at
        xPad - x padding between cells
        yPad - y padding between cells
      • makeCompactSpringGrid

        public static void makeCompactSpringGrid​(java.awt.Container parent,
                                                 int rows,
                                                 int cols,
                                                 int initialX,
                                                 int initialY,
                                                 int xPad,
                                                 int yPad)
        Aligns the first rows * cols components of parent in a grid. Each component in a column is as wide as the maximum preferred width of the components in that column; height is similarly determined for each row. The parent is made just big enough to fit them all.

        The code is based on one from the Sun Swing Tutorial Site. It was optimized and includes support for hidden components. Warning: the spring layout seems to accumulate information; this method should not be called many times on the same spring layout, it will substantially slow down the layout process. though it's not very elegant, to solve this problem - e.g. in TimelineFrame -, simply create set a panel's layout manager to a new SpringLayout before calling this method!

        Parameters:
        rows - number of rows
        cols - number of columns
        initialX - x location to start the grid at
        initialY - y location to start the grid at
        xPad - x padding between cells
        yPad - y padding between cells
      • getNoWriteIcon

        public static javax.swing.Icon getNoWriteIcon()
        Returns an Icon for a no-write or write-protection indicator. The icon has a dimension of 16x16 pixels with transparent background.
        Returns:
        the write-protected icon
      • createGradientPanel

        public static GradientPanel createGradientPanel()
      • constrainWidth

        public static void constrainWidth​(javax.swing.JComponent c,
                                          int w)
        Adjusts minimum, maximum and preferred size of a component so as to constrain its width to a given value.
        Parameters:
        c - the component to constrain
        w - the width in pixels
      • constrainHeight

        public static void constrainHeight​(javax.swing.JComponent c,
                                           int h)
        Adjusts minimum, maximum and preferred size of a component so as to constrain its height to a given value.
        Parameters:
        c - the component to constrain
        h - the height in pixels
      • constrainSize

        public static void constrainSize​(javax.swing.JComponent c,
                                         int w,
                                         int h)
        Sets minimum, maximum and preferred size to given values.
        Parameters:
        c - the component to constrain
        w - the width in pixels
        h - the height in pixels
      • wrapWindowBounds

        public static void wrapWindowBounds​(java.awt.Rectangle wr,
                                            java.awt.Rectangle sr)
      • setInitialDialogFocus

        public static void setInitialDialogFocus​(javax.swing.JComponent c)
        Passes keyboard focus to a given component when that component is to be presented in a dialog. This temporarily adds an AncestorListener to the component, detecting when its parent container is made visible. This is useful for defining an initial focus owner in JOptionPane calls for example.
        Parameters:
        c - the component to make focused once its parent container is shown
      • setAlwaysOnTop

        public static boolean setAlwaysOnTop​(java.awt.Component c,
                                             boolean b)
      • isAlwaysOnTop

        public static boolean isAlwaysOnTop​(java.awt.Component c)
      • convertPoint

        public static java.awt.Point convertPoint​(java.awt.Component source,
                                                  java.awt.Point aPoint,
                                                  java.awt.Component destination)
        Same as SwingUtilities.convertPoint, but handles JViewports properly
      • convertPointToScreen

        public static void convertPointToScreen​(java.awt.Point p,
                                                java.awt.Component c)
        Same as SwingUtilities.convertPointToScreen, but handles JViewports properly
      • convertPointFromScreen

        public static void convertPointFromScreen​(java.awt.Point p,
                                                  java.awt.Component c)
        Same as SwingUtilities.convertPointFromScreen, but handles JViewports properly
      • convertRectangle

        public static java.awt.Rectangle convertRectangle​(java.awt.Component source,
                                                          java.awt.Rectangle r,
                                                          java.awt.Component destination)
      • removeMenuModifierBindings

        public static void removeMenuModifierBindings​(javax.swing.JComponent c,
                                                      MenuGroup mg)
        Removes from a component's inputmap all bindings which may conflict with menu accelerators, that is those whose modifiers are equal to the secondary menu modifier, defined as control on mac and control+alt on windows and linux.
        Parameters:
        c - the component to inspect