0

What library can I use to find a first AWT component of a certain type? It's likely such a method has been written long ago. I need it for tests of my Swing GUI

If I can specify whether the search should be depth-first or breadth-first, it would be splendid

// my depth-first implementation
    @SuppressWarnings("unchecked")
    private <T extends Component> Optional<T> findChildByType(Container container, Class<T> childType) {
        for (Component component : container.getComponents()) {
            if (childType.isInstance(component))
                return Optional.of((T) component);
            if (component instanceof Container) {
                Optional<T> childByType = findChildByType((Container) component, childType);
                if (childByType.isPresent()) return childByType;
            }
        }
        return Optional.empty();
    }

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.