/**
   * Requests focus for a component. If that's not possible it's {@link FocusTraversalPolicy}is
   * checked. If that doesn't work all it's children is recursively checked with this method.
   *
   * @param component the component to request focus for
   * @return the component which has focus or probably will obtain focus, null if no component will
   *     receive focus
   */
  public static Component smartRequestFocus(Component component) {
    Component componentTemp = component;
    if (requestFocus(componentTemp)) return componentTemp;

    if (componentTemp instanceof JComponent) {
      FocusTraversalPolicy policy = ((JComponent) componentTemp).getFocusTraversalPolicy();

      if (policy != null) {
        Component focusComponent = policy.getDefaultComponent((Container) componentTemp);

        if (focusComponent != null && requestFocus(focusComponent)) {
          return focusComponent;
        }
      }
    }

    if (componentTemp instanceof Container) {
      Component[] children = ((Container) componentTemp).getComponents();

      for (int i = 0; i < children.length; i++) {
        componentTemp = smartRequestFocus(children[i]);

        if (componentTemp != null) return componentTemp;
      }
    }

    return null;
  }
 public Component getDefaultComponent(Container focusCycleRoot) {
   if (delegatePolicy != null) {
     return delegatePolicy.getDefaultComponent(focusCycleRoot);
   } else {
     return getFirstComponent(focusCycleRoot);
   }
 }
示例#3
0
  public Component findNextFocus() {
    // Find focus owner
    Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
    Container root = c == null ? null : c.getFocusCycleRootAncestor();

    if (root != null) {
      FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
      Component nextFocus = policy.getComponentAfter(root, c);
      if (nextFocus == null) {
        nextFocus = policy.getDefaultComponent(root);
      }
      return nextFocus;
    }
    return null;
  }
示例#4
0
文件: AWTMisc.java 项目: petr-s/jogl
 /**
  * Traverse to the next forward or backward component using the container's FocusTraversalPolicy.
  *
  * @param comp the assumed current focuse component
  * @param forward if true, returns the next focus component, otherwise the previous one.
  * @return
  */
 public static Component getNextFocus(Component comp, boolean forward) {
   Container focusContainer = comp.getFocusCycleRootAncestor();
   while (focusContainer != null
       && (!focusContainer.isShowing()
           || !focusContainer.isFocusable()
           || !focusContainer.isEnabled())) {
     comp = focusContainer;
     focusContainer = comp.getFocusCycleRootAncestor();
   }
   Component next = null;
   if (focusContainer != null) {
     final FocusTraversalPolicy policy = focusContainer.getFocusTraversalPolicy();
     next =
         forward
             ? policy.getComponentAfter(focusContainer, comp)
             : policy.getComponentBefore(focusContainer, comp);
     if (next == null) {
       next = policy.getDefaultComponent(focusContainer);
     }
   }
   return next;
 }
示例#5
0
 public Component getDefaultComponent(Container root) {
   return defaultPolicy.getDefaultComponent(root);
 }