Package io.github.jonestimd.swing
Class ComponentTreeUtils
- java.lang.Object
-
- io.github.jonestimd.swing.ComponentTreeUtils
-
public class ComponentTreeUtils extends java.lang.Object
Utility methods for traversing a Swing component hierarchy.
-
-
Constructor Summary
Constructors Constructor Description ComponentTreeUtils()
-
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>
CfindComponent(java.awt.Container container, java.lang.Class<C> type)
Find a component in a container.static <C extends java.awt.Component>
CfindComponent(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.
-
-
-
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 containervisitor
- 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 containervisitor
- the consumer to receive each componentdescend
- 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 containertype
- the type of components to pass tovisitor
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 containertype
- the type of components to pass tovisitor
visitor
- the consumer to receive matching componentsdescend
- 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 searchtype
- 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 searchtype
- the class of the componentpredicate
- 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 componenttype
- 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 componenttype
- the required type of the ancestordefaultValue
- 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 componentcondition
- 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 componentmaybeAncestor
- the potential ancestor component- Returns:
- true if
maybeAncestor
is an ancestor ofcomponent
-
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.
-
-