Example usage for java.awt Container setFocusCycleRoot

List of usage examples for java.awt Container setFocusCycleRoot

Introduction

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

Prototype

public void setFocusCycleRoot(boolean focusCycleRoot) 

Source Link

Document

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

Usage

From source file:Main.java

public static Component getNearestFocusableComponent(Component c, Container desiredRoot) {
    if (c == null)
        c = desiredRoot;//from  ww w.ja  v a  2 s  .  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;
}