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 JOptionPane parent./*w ww. j a  v  a2 s .c o  m*/
 *
 * @param c The component.
 *
 * @return The parent JOptionPane.
 */
public static JOptionPane getOptionPane(Component c) {
    Component parent = c;

    while (parent != null) {
        if (parent instanceof JOptionPane) {
            return (JOptionPane) parent;
        }

        parent = parent.getParent();
    }

    return null;
}

From source file:Main.java

/**
 * Get specified parent./*from   w  w  w  .  ja  v a  2 s  .  co  m*/
 * @param <T>
 * @param component
 * @param clazz
 * @return
 */
public static <T> T getParent(Component component, Class<T> clazz) {

    if (component == null) {
        return null;
    }

    if (clazz.isInstance(component)) {
        return (T) component;
    }

    return getParent(component.getParent(), clazz);
}

From source file:Utils.java

public static Window findWindow(Component c) {
    if (c == null) {
        return JOptionPane.getRootFrame();
    } else if (c instanceof Window) {
        return (Window) c;
    } else {/*w w w  . j  av a2  s .  c  om*/
        return findWindow(c.getParent());
    }
}

From source file:Main.java

public static MouseEvent adaptEventToDescendent(MouseEvent e, JComponent descendentTarget) {
    Point trans = new Point();
    Component source = e.getComponent();

    Component current = descendentTarget;
    while (current != source) {
        Rectangle b = current.getBounds();
        trans.x += b.x;/*  w w w.  j a  v  a 2 s  .c o  m*/
        trans.y += b.y;
        current = current.getParent();
    }
    Point point = e.getPoint();

    return new MouseEvent(descendentTarget, e.getID(), e.getWhen(), e.getModifiers(), point.x + trans.x,
            point.y + trans.y, e.getClickCount(), e.isPopupTrigger(), e.getButton());
}

From source file:Main.java

/**
 * Returns window ancestor for specified component or null if it doesn't
 * exist.//from w w w.j  a  va 2s . c  o m
 *
 * @param component
 *            component to process
 * @return window ancestor for specified component or null if it doesn't
 *         exist
 */
public static Window getWindowAncestor(final Component component) {
    if (component == null) {
        return null;
    }
    if (component instanceof Window) {
        return (Window) component;
    }
    for (Container p = component.getParent(); p != null; p = p.getParent()) {
        if (p instanceof Window) {
            return (Window) p;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Gets the window or the internal frame holding the component.
 *
 * @param component/*w  w w  .  j a  v  a  2s . com*/
 *          the component to look the window or internal frame for.
 * @return the window (frame or dialog) or the internal frame in the component
 *         hierarchy.
 */
public static Component getWindowOrInternalFrame(Component component) {
    if ((component instanceof Window) || (component instanceof JInternalFrame)) {
        return component;
    }
    if (component != null) {
        return getWindowOrInternalFrame(component.getParent());
    }
    return null;
}

From source file:Main.java

public static Frame getFrame(Component theComponent) {
    Component currParent = theComponent;
    Frame theFrame = null;// w w  w  .j a v a  2  s. c  om
    while (currParent != null) {
        if (currParent instanceof Frame) {
            theFrame = (Frame) currParent;
            break;
        }
        currParent = currParent.getParent();
    }
    return theFrame;
}

From source file:Main.java

public static Frame getParent(Component theComponent) {
    // this finds the topmost ancestor parent of a component,;;
    // so that we can give that parent to dialog constructors...;;
    Component currParent = theComponent;
    Frame theFrame = null;/*  www.  j a v a 2s.c  o  m*/
    while (currParent != null) {
        if (currParent instanceof Frame) {
            theFrame = (Frame) currParent;
            break;
        }
        ;
        currParent = currParent.getParent();
    }
    ;
    return theFrame;
}

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

/**
 *
 * @param c//from   ww w  .  j a  v a 2 s.  c o  m
 * @return
 */
public static Window findMainframe(Component c) {
    if (c instanceof TestCreator) {
        return (Window) c;
    } else {
        return findMainframe(c.getParent());
    }
}

From source file:Main.java

private static java.awt.Point getLocation(java.awt.Point rv, java.awt.Component c,
        java.awt.Component ancestor) {
    assert c != null;
    if (c == ancestor) {
        return rv;
    } else {//from   w  w  w .j a v  a2  s .c o  m
        rv.x += c.getX();
        rv.y += c.getY();
        return getLocation(rv, c.getParent(), ancestor);
    }
}