Example usage for java.awt Container getFocusTraversalPolicy

List of usage examples for java.awt Container getFocusTraversalPolicy

Introduction

In this page you can find the example usage for java.awt Container getFocusTraversalPolicy.

Prototype

public FocusTraversalPolicy getFocusTraversalPolicy() 

Source Link

Document

Returns the focus traversal policy that will manage keyboard traversal of this Container's children, or null if this Container is not a focus cycle root.

Usage

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);
    }/* www  .  ja  va2  s.com*/
    return prevFocus;
}

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);
    }/* ww w. j  a  va 2s .  c  o m*/
    return nextFocus;
}

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 2s . com*/
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: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 w ww  .  ja v  a2 s.c  om*/
                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 {//from w w w.jav a  2 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);
}

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

private Component getComponent(int index) {
    Component c = components.get(index);
    Container cc = null;
    if (c instanceof Container) {
        cc = (Container) c;/*from   w w w  .  ja v  a  2  s  .  c o  m*/
        if (cc.isFocusTraversalPolicyProvider() || cc.isFocusCycleRoot())
            c = cc.getFocusTraversalPolicy().getFirstComponent(cc);
        else if (cc instanceof JScrollPane) {
            if (((JScrollPane) cc).getViewport().getComponentCount() > 0)
                c = ((JScrollPane) cc).getViewport().getComponent(0);
        }
    }
    return c != null ? c : cc;
}

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

/**
 * @param component//from w  w w  . j  a v a 2s .c  o m
 * @return
 */
@SuppressWarnings("unused")
private FocusTraversalPolicy getFocusTraversalPolicyForComponent(Component component) {
    Container c = null;
    while ((c = component.getParent()) != null) {
        if (c.isFocusTraversalPolicyProvider())
            return c.getFocusTraversalPolicy();
        component = c;
    }

    return null;
}