Class ComponentTreeUtils


  • public class ComponentTreeUtils
    extends java.lang.Object
    Utility methods for traversing a Swing component hierarchy.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T findAncestor​(java.awt.Component component, java.lang.Class<T> type)
      Find an ancestor of a component.
      static <T> T findAncestor​(java.awt.Component component, java.lang.Class<T> type, T defaultValue)
      Find an ancestor of a component.
      static java.util.Optional<java.awt.Component> findAncestor​(java.awt.Component component, java.util.function.Predicate<java.awt.Component> condition)
      Find an ancestor of a component that matches a condition.
      static <C extends java.awt.Component>
      C
      findComponent​(java.awt.Container container, java.lang.Class<C> type)
      Find a component in a container.
      static <C extends java.awt.Component>
      C
      findComponent​(java.awt.Container container, java.lang.Class<C> type, java.util.function.Predicate<C> predicate)
      Find a component in a container.
      static java.awt.Component getParent​(java.awt.Component component)
      Return the parent of a component.
      static boolean isAncestor​(java.awt.Component component, java.awt.Component maybeAncestor)
      Check if one component is the ancestor of another component.
      static <C> void visitComponentTree​(java.awt.Container rootComponent, java.lang.Class<C> type, java.util.function.Consumer<? super C> visitor)
      Traverse a container and all its descendants, passing components matching a type to a consumer.
      static <C> void visitComponentTree​(java.awt.Container rootComponent, java.lang.Class<C> type, java.util.function.Consumer<? super C> visitor, java.util.function.Predicate<java.awt.Container> descend)
      Traverse a container and some of its descendants, passing components matching a type to a consumer.
      static void visitComponentTree​(java.awt.Container rootComponent, java.util.function.Consumer<java.awt.Component> visitor)
      Traverse a container and all its descendants, passing each component to a consumer.
      static void visitComponentTree​(java.awt.Container rootComponent, java.util.function.Consumer<java.awt.Component> visitor, java.util.function.Predicate<java.awt.Container> descend)
      Traverse a container and some of its descendants, passing each component to a consumer.
      • Methods inherited from class java.lang.Object

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

      • ComponentTreeUtils

        public ComponentTreeUtils()
    • Method Detail

      • visitComponentTree

        public static void visitComponentTree​(java.awt.Container rootComponent,
                                              java.util.function.Consumer<java.awt.Component> visitor)
        Traverse a container and all its descendants, passing each component to a consumer.
        Parameters:
        rootComponent - the container
        visitor - the consumer to receive each component
      • visitComponentTree

        public static void visitComponentTree​(java.awt.Container rootComponent,
                                              java.util.function.Consumer<java.awt.Component> visitor,
                                              java.util.function.Predicate<java.awt.Container> descend)
        Traverse a container and some of its descendants, passing each component to a consumer.
        Parameters:
        rootComponent - the container
        visitor - the consumer to receive each component
        descend - a predicate that controls which containers to traverse
      • visitComponentTree

        public static <C> void visitComponentTree​(java.awt.Container rootComponent,
                                                  java.lang.Class<C> type,
                                                  java.util.function.Consumer<? super C> visitor)
        Traverse a container and all its descendants, passing components matching a type to a consumer.
        Parameters:
        rootComponent - the container
        type - the type of components to pass to visitor
        visitor - the consumer to receive matching components
      • visitComponentTree

        public static <C> void visitComponentTree​(java.awt.Container rootComponent,
                                                  java.lang.Class<C> type,
                                                  java.util.function.Consumer<? super C> visitor,
                                                  java.util.function.Predicate<java.awt.Container> descend)
        Traverse a container and some of its descendants, passing components matching a type to a consumer.
        Parameters:
        rootComponent - the container
        type - the type of components to pass to visitor
        visitor - the consumer to receive matching components
        descend - a predicate that controls which containers to traverse
      • findComponent

        public static <C extends java.awt.Component> C findComponent​(java.awt.Container container,
                                                                     java.lang.Class<C> type)
        Find a component in a container.
        Parameters:
        container - the container to search
        type - the class of the component to find
        Returns:
        the first descendant component of the specified type
      • findComponent

        public static <C extends java.awt.Component> C findComponent​(java.awt.Container container,
                                                                     java.lang.Class<C> type,
                                                                     java.util.function.Predicate<C> predicate)
        Find a component in a container.
        Parameters:
        container - the container to search
        type - the class of the component
        predicate - a condition to be met by the component
        Returns:
        the first descendant component of the specified type for which predicate is true
      • findAncestor

        public static <T> T findAncestor​(java.awt.Component component,
                                         java.lang.Class<T> type)
        Find an ancestor of a component.
        Parameters:
        component - the starting component
        type - the required type of the ancestor
        Returns:
        the first ancestor of the specified type
      • findAncestor

        public static <T> T findAncestor​(java.awt.Component component,
                                         java.lang.Class<T> type,
                                         T defaultValue)
        Find an ancestor of a component.
        Parameters:
        component - the starting component
        type - the required type of the ancestor
        defaultValue - the value to use of no ancestor is found
        Returns:
        the first ancestor of the specified type or defaultValue
      • findAncestor

        public static java.util.Optional<java.awt.Component> findAncestor​(java.awt.Component component,
                                                                          java.util.function.Predicate<java.awt.Component> condition)
        Find an ancestor of a component that matches a condition.
        Parameters:
        component - the starting component
        condition - the condition to match
        Returns:
        the first ancestor for which condition is true
      • isAncestor

        public static boolean isAncestor​(java.awt.Component component,
                                         java.awt.Component maybeAncestor)
        Check if one component is the ancestor of another component.
        Parameters:
        component - the descendant component
        maybeAncestor - the potential ancestor component
        Returns:
        true if maybeAncestor is an ancestor of component
      • getParent

        public static java.awt.Component getParent​(java.awt.Component component)
        Return the parent of a component. If the component is a popup menu and its parent is null then return its invoker.