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

/**
 * Returns the first parent of given component which is an instance of given class.
 *//* w  ww. j a  va2 s  .c  o  m*/
public static <T> T getParent(Component aComponent, Class<T> aClass) {
    while (aComponent != null && !aClass.isInstance(aComponent))
        aComponent = aComponent.getParent();
    return (T) aComponent;
}

From source file:Main.java

/**
 * Return the Frame of a specified component if any.
 * //from   www .  j  a  v  a2 s .com
 * @param component
 *            the specified component
 * 
 * @return the Frame of a specified component if any; otherwise null
 */
public static Frame getFrame(Component component) {
    Frame frame;
    Component parent;
    while ((parent = component.getParent()) != null) {
        component = parent;
    }
    if (component instanceof Frame) {
        frame = (Frame) component;
    } else {
        frame = null;
    }
    return frame;
}

From source file:Main.java

public static JFrame findParentJFrame(JComponent c) {
    if (c == null)
        return null;
    Component parent = c.getParent();
    while (!(parent instanceof JFrame) && (parent != null)) {
        parent = parent.getParent();
    }/*from  ww w . j a  v a 2  s  . c  o m*/
    return (JFrame) parent;
}

From source file:Main.java

public static Container getRootContainer(Component c) {
    if (c == null) {
        return null;
    }/* w w w. j  a  v  a  2  s .  co m*/
    Container parent = c.getParent();
    while ((parent != null) && !(parent instanceof JPopupMenu) && !(parent instanceof JInternalFrame)
            && !(parent instanceof Window) && (parent.getParent() != null)) {
        parent = parent.getParent();
    }
    return parent;
}

From source file:Main.java

public static boolean isComponentVisible(Component comp) {
    if (!comp.isVisible() && !isRootComponent(comp)) {
        return false;
    }//from  w w  w. j  av a  2 s . com
    Component parent = comp.getParent();

    return parent == null || isComponentVisible(parent);

}

From source file:Main.java

static public Window getParentContainer(Component component) {
    if (component instanceof Window) {
        return (Window) component;
    }//from w  ww.j  a va 2 s  . c o m
    while (component.getParent() != null) {
        component = component.getParent();
        if (component instanceof Window) {
            return (Window) component;
        }
    }
    if (component instanceof Window) {
        return (Window) component;
    } else {
        return null;
    }
}

From source file:Main.java

public static JTabbedPane findParentJTabbedPane(JComponent c) {
    if (c == null)
        return null;
    Component parent = c.getParent();
    while (!(parent instanceof JTabbedPane) && (parent != null)) {
        parent = parent.getParent();
    }// w ww.j a va 2  s . c  om
    return (JTabbedPane) parent;
}

From source file:Main.java

/**
 * Workaround for bug in swing invalidate/validate concept. Swing is caching
 * component sizes and clearing that cache once invalidate is called, which is
 * fine. But in case a validate is in progress and part of the ancestor tree
 * is already layouted based on old sizes then the caches are already set, but
 * the valid flag is still false. Therefore this method can be called which
 * will invalidate all ancestors *including* those that are still invalid in
 * order to clear all size caches. see also invalidateSubTree()
 *///from   w  w w. j av a2 s  .  co  m
public static void invalidateAncestors(Component c) {
    if (c != null) {
        while (true) {
            c.invalidate();
            c = c.getParent();
            if (c == null) {
                break;
            }
            if (c instanceof JComponent && ((JComponent) c).isValidateRoot()) {
                break;
            }
        }
    }
}

From source file:Main.java

public static Frame getOwnerFrame(Component component) {
    if (component instanceof Frame) {
        return (Frame) component;
    }//  ww  w  . ja v  a2s.co m
    Container parent = component.getParent();
    while (!(parent instanceof Frame)) {
        parent = parent.getParent();
        if (parent == null) {
            throw new IllegalArgumentException("Component has no root Frame.");
        }
    }
    return (Frame) parent;
}

From source file:Main.java

public static Rectangle getRelativeBounds(Component component) {
    Rectangle bounds = new Rectangle(0, 0, component.getWidth(), component.getHeight());
    Container parent = component.getParent();

    while (parent != null) {
        bounds.x += component.getX();/*  www.ja  va 2s .  c o m*/
        bounds.y += component.getY();
        component = parent;
        parent = component.getParent();
    }

    return bounds;
}