Package ucar.ui.util

Class SwingUtils


  • public final class SwingUtils
    extends Object
    A collection of utility methods for Swing.
    See Also:
    "http://tips4java.wordpress.com/2008/11/13/swing-utils/"
    • Field Detail

      • NOT_NULL

        public static final Object NOT_NULL
    • Method Detail

      • getDescendantsOfType

        public static <T extends JComponentList<T> getDescendantsOfType​(Class<T> clazz,
                                                                          Container container)
        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.

        This method invokes getDescendantsOfType(clazz, container, true)

        Parameters:
        clazz - the class of components whose instances are to be found.
        container - the container at which to begin the search
        Returns:
        the List of components
      • getDescendantsOfType

        public static <T extends JComponentList<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.

        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
      • getDescendantOfType

        public static <T extends JComponent> T getDescendantOfType​(Class<T> clazz,
                                                                   Container container,
                                                                   String property,
                                                                   Object value)
                                                            throws IllegalArgumentException
        Convenience method that searches below container in the component hierarchy and returns the first found component that is an instance of class clazz having the bound property value. Returns null if such component cannot be found.

        This method invokes getDescendantOfType(clazz, container, property, value, true)

        Parameters:
        clazz - the class of component whose instance is to be found.
        container - the container at which to begin the search
        property - the className of the bound property, exactly as expressed in the accessor e.g. "Text" for getText(), "Value" for getValue().
        value - the value of the bound property
        Returns:
        the component, or null if no such component exists in the container
        Throws:
        IllegalArgumentException - if the bound property does not exist for the class or cannot be accessed
      • getDescendantOfType

        public static <T extends JComponent> T getDescendantOfType​(Class<T> clazz,
                                                                   Container container,
                                                                   String property,
                                                                   Object value,
                                                                   boolean nested)
                                                            throws IllegalArgumentException
        Convenience method that searches below container in the component hierarchy and returns the first found component that is an instance of class clazz and has the bound property value. Returns null if such component cannot be found.
        Parameters:
        clazz - the class of component whose instance to be found.
        container - the container at which to begin the search
        property - the className of the bound property, exactly as expressed in the accessor e.g. "Text" for getText(), "Value" for getValue().
        value - the value of the bound property
        nested - true to list components nested within another component which is also an instance of clazz, false otherwise
        Returns:
        the component, or null if no such component exists in the container
        Throws:
        IllegalArgumentException - if the bound property does not exist for the class or cannot be accessed
      • getDescendantsOfClass

        public static <T extends JComponentList<T> getDescendantsOfClass​(Class<T> clazz,
                                                                           Container container)
        Convenience method for searching below container in the component hierarchy and return nested components of class clazz it finds. Returns an empty list if no such components exist in the container.

        This method invokes getDescendantsOfClass(clazz, container, true)

        Parameters:
        clazz - the class of components to be found.
        container - the container at which to begin the search
        Returns:
        the List of components
      • getDescendantsOfClass

        public static <T extends JComponentList<T> getDescendantsOfClass​(Class<T> clazz,
                                                                           Container container,
                                                                           boolean nested)
        Convenience method for searching below container in the component hierarchy and return nested components of class clazz it finds. Returns an empty list if no such components exist in the container.
        Parameters:
        clazz - the class of components 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
      • getDescendantOfClass

        public static <T extends JComponent> T getDescendantOfClass​(Class<T> clazz,
                                                                    Container container,
                                                                    String property,
                                                                    Object value)
                                                             throws IllegalArgumentException
        Convenience method that searches below container in the component hierarchy in a depth first manner and returns the first found component of class clazz having the bound property value.

        Returns null if such component cannot be found.

        This method invokes getDescendantOfClass(clazz, container, property, value, true)

        Parameters:
        clazz - the class of component to be found.
        container - the container at which to begin the search
        property - the className of the bound property, exactly as expressed in the accessor e.g. "Text" for getText(), "Value" for getValue(). This parameter is case sensitive.
        value - the value of the bound property
        Returns:
        the component, or null if no such component exists in the container's hierarchy.
        Throws:
        IllegalArgumentException - if the bound property does not exist for the class or cannot be accessed
      • getDescendantOfClass

        public static <T extends JComponent> T getDescendantOfClass​(Class<T> clazz,
                                                                    Container container,
                                                                    String property,
                                                                    Object value,
                                                                    boolean nested)
                                                             throws IllegalArgumentException
        Convenience method that searches below container in the component hierarchy in a depth first manner and returns the first found component of class clazz having the bound property value.

        Returns null if such component cannot be found.

        Parameters:
        clazz - the class of component to be found.
        container - the container at which to begin the search
        property - the className of the bound property, exactly as expressed in the accessor e.g. "Text" for getText(), "Value" for getValue(). This parameter is case sensitive.
        value - the value of the bound property
        nested - true to include components nested within another listed component, false otherwise
        Returns:
        the component, or null if no such component exists in the container's hierarchy
        Throws:
        IllegalArgumentException - if the bound property does not exist for the class or cannot be accessed
      • equals

        public static boolean equals​(Object obj1,
                                     Object obj2)
        Convenience method for determining whether two objects are either equal or both null.
        Parameters:
        obj1 - the first reference object to compare.
        obj2 - the second reference object to compare.
        Returns:
        true if obj1 and obj2 are equal or if both are null, false otherwise
      • getComponentMap

        public static Map<JComponent,​List<JComponent>> getComponentMap​(JComponent container,
                                                                             boolean nested)
        Convenience method for mapping a container in the hierarchy to its contained components. The keys are the containers, and the values are lists of contained components.

        Implementation note: The returned value is a HashMap and the values are of type ArrayList. This is subject to change, so callers should code against the interfaces Map and List.

        Parameters:
        container - The JComponent to be mapped
        nested - true to drill down to nested containers, false otherwise
        Returns:
        the Map of the UI
      • getUIDefaultsOfClass

        public static UIDefaults getUIDefaultsOfClass​(Class clazz)
        Convenience method for retrieving a subset of the UIDefaults pertaining to a particular class.
        Parameters:
        clazz - the class of interest
        Returns:
        the UIDefaults of the class
      • getUIDefaultsOfClass

        public static UIDefaults getUIDefaultsOfClass​(String className)
        Convenience method for retrieving a subset of the UIDefaults pertaining to a particular class.
        Parameters:
        className - fully qualified name of the class of interest
        Returns:
        the UIDefaults of the class named
      • getUIDefaultOfClass

        public static Object getUIDefaultOfClass​(Class clazz,
                                                 String property)
        Convenience method for retrieving the UIDefault for a single property of a particular class.
        Parameters:
        clazz - the class of interest
        property - the property to query
        Returns:
        the UIDefault property, or null if not found
      • getProperties

        public static Map<Object,​Object> getProperties​(JComponent component)
        Convenience method for obtaining most non-null human readable properties of a JComponent. Array properties are not included.

        Implementation note: The returned value is a HashMap. This is subject to change, so callers should code against the interface Map.

        Parameters:
        component - the component whose proerties are to be determined
        Returns:
        the class and value of the properties
      • getJClass

        public static <T extends JComponentClass getJClass​(T component)
        Convenience method to obtain the Swing class from which this component was directly or indirectly derived.
        Parameters:
        component - The component whose Swing superclass is to be determined
        Returns:
        The nearest Swing class in the inheritance tree