Example usage for java.awt Container isFocusCycleRoot

List of usage examples for java.awt Container isFocusCycleRoot

Introduction

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

Prototype

public boolean isFocusCycleRoot() 

Source Link

Document

Returns whether this Container is the root of a focus traversal cycle.

Usage

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 . ja v a 2  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:Main.java

public static Component getNearestFocusableComponent(Component c, Container desiredRoot) {
    if (c == null)
        c = desiredRoot;/*  w w  w .j  av  a 2s.c  o m*/
    if (c == null)
        c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();

    boolean cachedFocusCycleRoot = false;
    // make the desiredRoot into a focusCycleRoot
    if (desiredRoot != null) {
        cachedFocusCycleRoot = desiredRoot.isFocusCycleRoot();
        if (!cachedFocusCycleRoot)
            desiredRoot.setFocusCycleRoot(true);
    }

    Container focusRoot = null;
    if (c instanceof Container) {
        Container cnt = (Container) c;
        focusRoot = cnt.isFocusCycleRoot(cnt) ? cnt : cnt.getFocusCycleRootAncestor();
    } else
        focusRoot = c.getFocusCycleRootAncestor();

    Component focuser = null;
    if (focusRoot != null)
        //zw, remarked - selected component should become focused
        //focuser = focusRoot.getFocusTraversalPolicy().getLastComponent(focusRoot);
        focuser = c; //zw, added - selected component should become focused

    // restore the desiredRoot to its previous state
    if (desiredRoot != null && !cachedFocusCycleRoot) {
        desiredRoot.setFocusCycleRoot(cachedFocusCycleRoot);
    }
    return focuser;
}

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;//  ww w. j  a va 2 s. com
        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;
}