List of usage examples for java.awt Container getFocusCycleRootAncestor
public Container getFocusCycleRootAncestor()
From source file:Main.java
public static Component getNearestFocusableComponent(Component c, Container desiredRoot) { if (c == null) c = desiredRoot;// w w w . j ava2s. co 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: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 w w . ja v a2 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; }