Example usage for java.awt FocusTraversalPolicy getFirstComponent

List of usage examples for java.awt FocusTraversalPolicy getFirstComponent

Introduction

In this page you can find the example usage for java.awt FocusTraversalPolicy getFirstComponent.

Prototype

public abstract Component getFirstComponent(Container aContainer);

Source Link

Document

Returns the first Component in the traversal cycle.

Usage

From source file:Main.java

/**
 * Returns first focusable component found in the container.
 *
 * @param container container to process
 * @return first focusable component found in the container
 *//*from  w  ww  .  j  a  v a  2  s.co m*/
public static Component findFocusableComponent(final Container container) {
    final FocusTraversalPolicy focusTraversalPolicy = container.getFocusTraversalPolicy();
    if (focusTraversalPolicy != null) {
        return focusTraversalPolicy.getFirstComponent(container);
    } else {
        for (final Component component : container.getComponents()) {
            if (component.isFocusable()) {
                return component;
            }
            if (component instanceof Container) {
                final Component focusable = findFocusableComponent((Container) component);
                if (focusable != null) {
                    return focusable;
                }
            }
        }
        return null;
    }
}

From source file:org.jdal.swing.form.FormFocusTransversalPolicy.java

@Override
public Component getComponentAfter(Container container, Component component) {

    // Fix awt bug looking for ComboBoxEditor instead ComboBox
    // see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6205817
    if (component.getParent() instanceof JComboBox)
        component = component.getParent();

    int index = components.indexOf(component);

    if (index == -1) { // not owner
        Container childContainer = getTopmostProvider(container, component);
        if (childContainer == null)
            return getFirstComponent(container);

        FocusTraversalPolicy ftp = childContainer.getFocusTraversalPolicy();
        if (ftp != null && ftp != this) {
            Component next = ftp.getComponentAfter(childContainer, component);
            if (next != ftp.getFirstComponent(container))
                return next;

            // child cycle
            do {//  ww  w .  j  a  v a 2  s. co m
                index = components.indexOf(childContainer);
                childContainer = childContainer.getParent();
            } while (index == -1 || childContainer == null);

            if (index == -1) {
                log.warn("I can't figure what is the next component");
                return getFirstComponent(container);
            }
        }
    }

    index++;

    if (index < components.size() && index >= 0) {
        Component c = getComponent(index);
        if (c.isEnabled() && c.isFocusable())
            return c;
        else
            return getComponentAfter(container, c);
    }

    return getFirstComponent(container);
}