Java Code Examples for java.awt.Component#getFocusCycleRootAncestor()

The following examples show how to use java.awt.Component#getFocusCycleRootAncestor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NbPresenter.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Requests focus for <code>currentMessage</code> component.
 * If it is of <code>JComponent</code> type it tries default focus
 * request first. */
private void requestFocusForMessage() {
    Component comp = currentMessage;

    if(comp == null) {
        return;
    }

    if (/*!Constants.AUTO_FOCUS &&*/ FocusManager.getCurrentManager().getActiveWindow() == null) {
        // Do not steal focus if no Java window have it
        Component defComp = null;
        Container nearestRoot =
            (comp instanceof Container && ((Container) comp).isFocusCycleRoot()) ? (Container) comp : comp.getFocusCycleRootAncestor();
        if (nearestRoot != null) {
            defComp = nearestRoot.getFocusTraversalPolicy().getDefaultComponent(nearestRoot);
        }
        if (defComp != null) {
            defComp.requestFocusInWindow();
        } else {
            comp.requestFocusInWindow();
        }
    } else {
        if (!(comp instanceof JComponent)
            || !((JComponent)comp).requestDefaultFocus()) {

            comp.requestFocus();
        }
    }
}
 
Example 2
Source File: FocusArrowListener.java    From pumpernickel with MIT License 5 votes vote down vote up
/**
 * Returns a set of all the components that can have the keyboard focus.
 * <P>
 * My first implementation involved of this concept simply involved asking
 * JCompnonents if they were focusable, but in the
 * <code>FilledButtonTest</code> this resulted in shifting focus to the
 * ContentPane. Although it is technically focusable: if I used the tab key
 * I did <i>not</i> get this result. So I studied the inner workings for
 * Component.transferFocus() and ended up with a method that involved calls
 * to <code>getFocusCycleRootAncestor()</code>, and
 * <code>getFocusTraversalPolicy()</code>.
 * <P>
 * (Also credit goes to Werner for originally tipping me off towards looking
 * at FocusTraversalPolicies.)
 * 
 * @param currentFocusOwner
 *            the current focus owner.
 * @return all the JComponents that can receive the focus.
 */
public static Set<Component> getFocusableComponents(
		Component currentFocusOwner) {
	HashSet<Component> set = new HashSet<Component>();
	set.add(currentFocusOwner);

	Container rootAncestor = currentFocusOwner.getFocusCycleRootAncestor();
	Component comp = currentFocusOwner;
	while (rootAncestor != null
			&& !(rootAncestor.isShowing() && rootAncestor.isFocusable() && rootAncestor
					.isEnabled())) {
		comp = rootAncestor;
		rootAncestor = comp.getFocusCycleRootAncestor();
	}
	if (rootAncestor != null) {
		FocusTraversalPolicy policy = rootAncestor
				.getFocusTraversalPolicy();
		Component toFocus = policy.getComponentAfter(rootAncestor, comp);

		while (toFocus != null && set.contains(toFocus) == false) {
			set.add(toFocus);
			toFocus = policy.getComponentAfter(rootAncestor, toFocus);
		}

		toFocus = policy.getComponentBefore(rootAncestor, comp);

		while (toFocus != null && set.contains(toFocus) == false) {
			set.add(toFocus);
			toFocus = policy.getComponentBefore(rootAncestor, toFocus);
		}
	}
	return set;
}
 
Example 3
Source File: FocusArrowListener.java    From PyramidShader with GNU General Public License v3.0 5 votes vote down vote up
/** Returns a set of all the components that
 * can have the keyboard focus.
 * <P>My first implementation involved of this concept
 * simply involved asking JCompnonents if they were
 * focusable, but in the <code>FilledButtonTest</code> this
 * resulted in shifting focus to the ContentPane.  Although
 * it is technically focusable: if I used the tab key
 * I did <i>not</i> get this result.  So I studied
 * the inner workings for Component.transferFocus()
 * and ended up with a method that involved
 * calls to <code>getFocusCycleRootAncestor()</code>,
 * and <code>getFocusTraversalPolicy()</code>.
 * <P>(Also credit goes to Werner for originally tipping me off
 * towards looking at FocusTraversalPolicies.)
 * @param currentFocusOwner the current focus owner.
 * @return all the JComponents that can receive the focus.
 */
public static Set<Component> getFocusableComponents(Component currentFocusOwner) {
	HashSet<Component> set = new HashSet<Component>();
	set.add(currentFocusOwner);

       Container rootAncestor = currentFocusOwner.getFocusCycleRootAncestor();
       Component comp = currentFocusOwner;
       while (rootAncestor != null && 
              !(rootAncestor.isShowing() && 
                rootAncestor.isFocusable() && 
                rootAncestor.isEnabled())) 
       {
           comp = rootAncestor;
           rootAncestor = comp.getFocusCycleRootAncestor();
       }
       if (rootAncestor != null) {
           FocusTraversalPolicy policy =
               rootAncestor.getFocusTraversalPolicy();
           Component toFocus = policy.getComponentAfter(rootAncestor, comp);
           
           while(toFocus!=null && set.contains(toFocus)==false) {
           	set.add(toFocus);
               toFocus = policy.getComponentAfter(rootAncestor, toFocus);
           }
           
           toFocus = policy.getComponentBefore(rootAncestor, comp);
           
           while(toFocus!=null && set.contains(toFocus)==false) {
           	set.add(toFocus);
               toFocus = policy.getComponentBefore(rootAncestor, toFocus);
           }
       }
	return set;
}
 
Example 4
Source File: FocusArrowListener.java    From Pixelitor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns a set of all the components that
 * can have the keyboard focus.
 * <P>My first implementation involved of this concept
 * simply involved asking JCompnonents if they were
 * focusable, but in the <code>FilledButtonTest</code> this
 * resulted in shifting focus to the ContentPane.  Although
 * it is technically focusable: if I used the tab key
 * I did <i>not</i> get this result.  So I studied
 * the inner workings for Component.transferFocus()
 * and ended up with a method that involved
 * calls to <code>getFocusCycleRootAncestor()</code>,
 * and <code>getFocusTraversalPolicy()</code>.
 * <P>(Also credit goes to Werner for originally tipping me off
 * towards looking at FocusTraversalPolicies.)
 *
 * @param currentFocusOwner the current focus owner.
 * @return all the JComponents that can receive the focus.
 */
public static Set<Component> getFocusableComponents(Component currentFocusOwner) {
    Set<Component> set = new HashSet<>();
    set.add(currentFocusOwner);

    Container rootAncestor = currentFocusOwner.getFocusCycleRootAncestor();
    Component comp = currentFocusOwner;
    while (rootAncestor != null &&
            !(rootAncestor.isShowing() &&
                    rootAncestor.isFocusable() &&
                    rootAncestor.isEnabled())) {
        comp = rootAncestor;
        rootAncestor = comp.getFocusCycleRootAncestor();
    }
    if (rootAncestor != null) {
        FocusTraversalPolicy policy =
                rootAncestor.getFocusTraversalPolicy();
        Component toFocus = policy.getComponentAfter(rootAncestor, comp);

        while (toFocus != null && !set.contains(toFocus)) {
            set.add(toFocus);
            toFocus = policy.getComponentAfter(rootAncestor, toFocus);
        }

        toFocus = policy.getComponentBefore(rootAncestor, comp);

        while (toFocus != null && !set.contains(toFocus)) {
            set.add(toFocus);
            toFocus = policy.getComponentBefore(rootAncestor, toFocus);
        }
    }
    return set;
}