public class JComponentHierarchyNavigator extends Object implements TreeNavigator<JComponent>
TreeNavigator
for a Swing component hierarchy.
Note that getParent
will return an (optional) JComponent
. This is not possible
if the parent is not of a subtype (e.g. a JFrame
) in which case an empty Optional
is returned. Similarly, children of JComponent
s might not be JComponent
s themselves. For this reason
getChild
will return an empty Optional
for a child which is no
JComponent
. If this is undesired, consider using a ComponentHierarchyNavigator
instead.
This implementation is thread-safe in the sense that individual method calls will not fail if the component hierarchy is changed concurrently. But it can not prevent return values from getting stale so chaining calls might lead to unexpected results, e.g.:
JComponent component = ... if (getParent(component).isPresent()) { // the component has a parent, so it should have a child index, too; // but the component hierarchy may have changed, so 'indexPresent' may be false boolean indexPresent = getChildIndex(component).isPresent(); }Similarly:
JComponent parent = ... Optional<JComponent> child1 = getChild(parent, 0); Optional<JComponent> child2 = getChild(parent, 0); // if the component hierarchy changed between the two calls, this may be false boolean sameChildren = child1.equals(child2);
Constructor and Description |
---|
JComponentHierarchyNavigator() |
Modifier and Type | Method and Description |
---|---|
Optional<JComponent> |
getChild(JComponent parent,
int childIndex) |
OptionalInt |
getChildIndex(JComponent node) |
int |
getChildrenCount(JComponent parent) |
Optional<JComponent> |
getParent(JComponent child) |
public Optional<JComponent> getParent(JComponent child)
getParent
in interface TreeNavigator<JComponent>
child
- an node in the treeempty
is
returnedpublic OptionalInt getChildIndex(JComponent node)
getChildIndex
in interface TreeNavigator<JComponent>
node
- a node in the treeempty
is returnedpublic int getChildrenCount(JComponent parent)
getChildrenCount
in interface TreeNavigator<JComponent>
parent
- a node in the treepublic Optional<JComponent> getChild(JComponent parent, int childIndex)
getChild
in interface TreeNavigator<JComponent>
parent
- a node in the treechildIndex
- a non-negative number specifying the index of the requested childempty
is returnedThis documentation is licensed under CC-BY 4.0, attributed to Nicolai Parlog from CodeFX.