Example usage for java.awt Component getParent

List of usage examples for java.awt Component getParent

Introduction

In this page you can find the example usage for java.awt Component getParent.

Prototype

public Container getParent() 

Source Link

Document

Gets the parent of this component.

Usage

From source file:Main.java

/**
 * Return the first parent that is a <code>JFrame</code> or a <code>JDialog</code>.
 * /*from w  ww.  j  a  v a  2  s. c om*/
 * @return The parent frame or dialog.
 * @param cmp The start search component.
 */
public static Component getFirstParentFrameOrDialog(Component cmp) {
    while (cmp != null) {
        if (cmp instanceof JFrame || cmp instanceof JDialog) {
            return cmp;
        }
        cmp = cmp.getParent();
    }
    return null;
}

From source file:GUIUtils.java

public static boolean isWithinParent(Component wind) {
    if (wind == null) {
        throw new IllegalArgumentException("Null Component passed");
    }// w  w w. j  av  a2 s  .c  o m

    Rectangle windowBounds = wind.getBounds();
    Component parent = wind.getParent();
    Rectangle parentRect = null;
    if (parent != null) {
        parentRect = new Rectangle(parent.getSize());
    } else {
        // parentRect = new
        // Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        parentRect = getScreenBoundsFor(windowBounds);
    }

    // if (windowBounds.x > (parentRect.width - 20)
    // || windowBounds.y > (parentRect.height - 20)
    // || (windowBounds.x + windowBounds.width) < 20
    // || (windowBounds.y + windowBounds.height) < 20)
    // {
    // return false;
    // }
    if (windowBounds.x < (parentRect.x - 20) || windowBounds.y < (parentRect.y - 20)) {
        return false;
    }
    return true;
}

From source file:Main.java

/**
 * Returns whether a component is a direct descendant of another one. This method will traverse the component
 * hierarchy, following the 'parent' relation for normal components and the 'invoker' relation for JPopupMenu
 * instances.//from w ww. j  a  v a  2  s.  c  om
 *
 * @param supposedParent The supposed ancestor component
 * @param supposedChild  The supposed child component
 * @return if 'supposedChild' is a descendant of 'supposedParent'
 */
public static boolean isDescendant(final Component supposedParent, final Component supposedChild) {
    if (supposedParent == null)
        throw new IllegalArgumentException("Parameter 'supposedParent' must not be null!");
    if (supposedChild == null)
        throw new IllegalArgumentException("Parameter 'supposedChild' must not be null!");

    Component runner = supposedChild;
    while (runner != null) {
        if (runner == supposedParent)
            return true;
        if (runner instanceof JPopupMenu)
            runner = ((JPopupMenu) runner).getInvoker();
        else
            runner = runner.getParent();
    }
    return false;
}

From source file:Main.java

/**
 * Return the first ancestor of <code>component</code> (beginning with itself) that is an
 * {@link Class#isInstance(Object) instance of} <code>c</code>. If <code>component</code> is in
 * a popup, {@link JPopupMenu#getInvoker()} is used instead of {@link Component#getParent()}.
 * /* w  w w .j a  va 2s.c o m*/
 * @param <C> type of class.
 * @param c the searched for class.
 * @param component the component.
 * @return the first ancestor or <code>null</code> if none match.
 */
public static final <C> C getAncestorOrSelf(final Class<C> c, Component component) {
    while (component != null && !c.isInstance(component)) {
        if (component instanceof JPopupMenu) {
            // popups are in a JLayeredPane in the root pane
            component = ((JPopupMenu) component).getInvoker();
        } else {
            component = component.getParent();
        }
    }
    return c.cast(component);
}

From source file:Main.java

public static CharSequence getNamedPathToComponent(Component c) {
    StringBuilder sb = new StringBuilder();
    if (c instanceof JScrollPane) {
        sb.append(String.format(" -> %s", ((JScrollPane) c).getViewport().getView().getName()));
    }//from www  . j  a  va2s  .c om
    while (c != null) {
        if (c.getName() != null) {
            sb.append(String.format(" -> %s", c.getName()));
        }
        c = c.getParent();
    }
    return sb;
}

From source file:org.kuali.test.ui.utils.UIUtils.java

/**
 *
 * @param c//from   w w  w .  j  a v  a  2s .  c o  m
 * @return
 */
public static Window findWindow(Component c) {
    if (c == null) {
        return JOptionPane.getRootFrame();
    } else if (c instanceof Window) {
        return (Window) c;
    } else {
        return findWindow(c.getParent());
    }
}

From source file:Main.java

/**
 * Returns delegate {@code RepaintManager} for {@code component} hierarchy.
 *///from w  ww.  ja v a  2  s  .c om
public static RepaintManager getDelegateRepaintManager(Component component) {
    RepaintManager delegate = null;
    if (Boolean.TRUE == SunToolkit.targetToAppContext(component).get(DELEGATE_REPAINT_MANAGER_KEY)) {
        while (delegate == null && component != null) {
            while (component != null && !(component instanceof JComponent)) {
                component = component.getParent();
            }
            if (component != null) {
                delegate = (RepaintManager) ((JComponent) component)
                        .getClientProperty(DELEGATE_REPAINT_MANAGER_KEY);
                component = component.getParent();
            }

        }
    }
    return delegate;
}

From source file:Main.java

/**
 * Returns whether the component is part of the parent's
 * container hierarchy. If a parent in the chain is of type 
 * JPopupMenu, the parent chain of its invoker is walked.
 * /*from ww w.java2  s .c o  m*/
 * @param focusOwner
 * @param parent
 * @return true if the component is contained under the parent's 
 *    hierarchy, coping with JPopupMenus.
 */
public static boolean isDescendingFrom(Component focusOwner, Component parent) {
    while (focusOwner != null) {
        if (focusOwner instanceof JPopupMenu) {
            focusOwner = ((JPopupMenu) focusOwner).getInvoker();
            if (focusOwner == null) {
                return false;
            }
        }
        if (focusOwner == parent) {
            return true;
        }
        focusOwner = focusOwner.getParent();
    }
    return false;
}

From source file:AWTUtilities.java

/**
 * Returns the parent Frame of the specified <code>Component</code> or
 * <code>null</code> if none exists.
 *///ww w . ja  v a  2 s. c  o  m

public static Frame frameForComponent(Component component) {
    while (component != null) {
        if (component instanceof Frame)
            return (Frame) component;
        component = component.getParent();
    }

    return null;
}

From source file:org.pentaho.reporting.engine.classic.core.util.ComponentDrawable.java

/**
 * A private helper method that locates the Window to which the component is currently added.
 *
 * @param component//from   w ww.  j  a  v  a 2 s. c om
 *          the component for which the Window should be returned.
 * @return the AWT-Window that is the (possibly indirect) parent of this component.
 */
protected static Window getWindowAncestor(final Component component) {
    Component parent = component.getParent();
    while (parent != null) {
        if (parent instanceof Window) {
            return (Window) parent;
        }
        parent = parent.getParent();
    }
    return null;
}