Example usage for java.awt FocusTraversalPolicy getDefaultComponent

List of usage examples for java.awt FocusTraversalPolicy getDefaultComponent

Introduction

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

Prototype

public abstract Component getDefaultComponent(Container aContainer);

Source Link

Document

Returns the default Component to focus.

Usage

From source file:Main.java

public static Component findNextFocus() {
    Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
    Container root = c.getFocusCycleRootAncestor();

    FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
    Component nextFocus = policy.getComponentAfter(root, c);
    if (nextFocus == null) {
        nextFocus = policy.getDefaultComponent(root);
    }/*from   w w  w.  ja  v a 2 s .c o m*/
    return nextFocus;
}

From source file:Main.java

public static Component findPrevFocus() {
    Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
    Container root = c.getFocusCycleRootAncestor();

    FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
    Component prevFocus = policy.getComponentBefore(root, c);
    if (prevFocus == null) {
        prevFocus = policy.getDefaultComponent(root);
    }/* w w  w .j  av a2 s.c  om*/
    return prevFocus;
}

From source file:Main.java

public static Component compositeRequestFocus(Component component) {
    if (component instanceof Container) {
        Container container = (Container) component;
        if (container.isFocusCycleRoot()) {
            FocusTraversalPolicy policy = container.getFocusTraversalPolicy();
            Component comp = policy.getDefaultComponent(container);
            if (comp != null) {
                comp.requestFocus();//from   ww  w.ja  va  2 s .  com
                return comp;
            }
        }
        Container rootAncestor = container.getFocusCycleRootAncestor();
        if (rootAncestor != null) {
            FocusTraversalPolicy policy = rootAncestor.getFocusTraversalPolicy();
            Component comp = policy.getComponentAfter(rootAncestor, container);

            if (comp != null && SwingUtilities.isDescendingFrom(comp, container)) {
                comp.requestFocus();
                return comp;
            }
        }
    }
    if (component.isFocusable()) {
        component.requestFocus();
        return component;
    }
    return null;
}

From source file:Util.java

/**
 * Requests focus for a component. If that's not possible it's
 * {@link FocusTraversalPolicy}is checked. If that doesn't work all it's
 * children is recursively checked with this method.
 *
 * @param component the component to request focus for
 * @return the component which has focus or probably will obtain focus, null
 *         if no component will receive focus
 *//*from  w ww. j a  va2  s.  com*/
public static Component smartRequestFocus(Component component) {
    if (requestFocus(component))
        return component;

    if (component instanceof JComponent) {
        FocusTraversalPolicy policy = ((JComponent) component).getFocusTraversalPolicy();

        if (policy != null) {
            Component focusComponent = policy.getDefaultComponent((Container) component);

            if (focusComponent != null && requestFocus(focusComponent)) {
                return focusComponent;
            }
        }
    }

    if (component instanceof Container) {
        Component[] children = ((Container) component).getComponents();

        for (int i = 0; i < children.length; i++) {
            component = smartRequestFocus(children[i]);

            if (component != null)
                return component;
        }
    }

    return null;
}