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

public static Window getOwnerWindow(Component component) {
    if (component instanceof Window) {
        return (Window) component;
    }/*from   w w  w  .  j ava2 s.c om*/
    Container parent = component.getParent();
    while (!(parent instanceof Window)) {
        parent = parent.getParent();
        if (parent == null) {
            throw new IllegalArgumentException("Component has no root window.");
        }
    }
    return (Window) parent;
}

From source file:Main.java

/** Gets the containing window for the given component. */
public static Window getWindow(Component c) {
    while (c != null) {
        if (c instanceof Window)
            return (Window) c;
        c = c.getParent();
    }//w  w w .ja va 2 s .  c o m
    return null;
}

From source file:Main.java

public static JTabbedPane getJTabbedPaneAncestor(Component c) {
    while (c != null) {
        if (c instanceof JTabbedPane) {
            return (JTabbedPane) c;
        }/*w  w  w.ja v a 2 s.  c om*/
        c = c.getParent();
    }
    return null;
}

From source file:Main.java

/**
 * Finds the frame in which the specified component is located.
 *
 * @param component//from   ww  w  . jav  a  2s .c o m
 *            The component.
 * @return The frame or null if the component is not in a frame.
 */

public static Frame findFrame(final Component component) {
    if (component == null || component instanceof Frame)
        return (Frame) component;
    return findFrame(component.getParent());
}

From source file:Main.java

public static Component getTopLevelAncestor(Component c) {
    while (c != null) {
        if (c instanceof Window || c instanceof Applet)
            break;
        c = c.getParent();
    }//  w  ww.j  a v a 2s . c  o m
    return c;
}

From source file:Main.java

public static Frame findParentFrame(Component component) {
    if (component == null) {
        throw new NullPointerException("component == null");
    }// w  ww.j av a 2 s.  co m
    Container parent = component.getParent();
    while (parent != null) {
        if ((parent instanceof Frame)) {
            return (Frame) parent;
        }
        parent = parent.getParent();
    }
    return null;
}

From source file:Main.java

/**
 * An improved version of/*w w w.  j a  v a2  s . co  m*/
 * {@link SwingUtilities#getAncestorOfClass(Class, Component)}. This method
 * traverses {@code JPopupMenu} invoker and uses generics to return an
 * appropriately typed object.
 * 
 * @param <T>
 *            the type of ancestor to find
 * @param clazz
 *            the class instance of the ancestor to find
 * @param c
 *            the component to start the search from
 * @return an ancestor of the correct type or {@code null} if no such
 *         ancestor exists. This method also returns {@code null} if any
 *         parameter is {@code null}.
 */
@SuppressWarnings("unchecked")
public static <T> T getAncestor(Class<T> clazz, Component c) {
    if (clazz == null || c == null) {
        return null;
    }

    Component parent = c.getParent();

    while (parent != null && !(clazz.isInstance(parent))) {
        parent = c instanceof JPopupMenu ? ((JPopupMenu) c).getInvoker() : c.getParent();
    }

    return (T) parent;
}

From source file:Main.java

public static void parentWindowToFront(Component component) {
    if (component == null) {
        throw new NullPointerException("component == null");
    }//from   ww w .j  a  va2  s . c o  m
    Container parent = component.getParent();
    while (parent != null) {
        if ((parent instanceof Window)) {
            Window parentWindow = (Window) parent;
            parentWindow.toFront();
            return;
        }
        parent = parent.getParent();
    }
}

From source file:Main.java

public static Container findContainer(Component thisComp, Class<? extends Container> containerClass) {
    if (thisComp == null) {
        return null;
    }// w  ww  .  j  a  va  2 s  . co m
    Container parent = thisComp.getParent();
    do {
        parent = parent.getParent();
        if (parent != null && containerClass.isAssignableFrom(parent.getClass())) {
            return parent;
        }
    } while (parent != null);
    return null;
}

From source file:Main.java

/**
 * Returns the parent <code>JFrame</code> of the specified component.
 * //w  w  w .  ja  v  a2  s. co m
 * @param component the component to find the parent JFrame for.
 * @return the parent JFrame.
 */
public static JFrame getParentJFrame(Component component) {
    if ((component == null) || (component instanceof JFrame)) {
        return (JFrame) component;
    }
    Container c = component.getParent();
    while ((c != null) && !(c instanceof JFrame)) {
        c = c.getParent();
    }
    return (JFrame) c;
}