Example usage for java.awt FocusTraversalPolicy getComponentAfter

List of usage examples for java.awt FocusTraversalPolicy getComponentAfter

Introduction

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

Prototype

public abstract Component getComponentAfter(Container aContainer, Component aComponent);

Source Link

Document

Returns the Component that should receive the focus after aComponent.

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.j ava2  s  .com
    return nextFocus;
}

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();/*  ww  w .j a  v  a2s  .  c  o  m*/
                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: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 a2  s.  c  o  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);
}