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 final int getComponentIndex(Component component) {
    if (component != null && component.getParent() != null) {
        Container currentComponent = component.getParent();
        for (int index = 0; index < currentComponent.getComponentCount(); index++) {
            if (currentComponent.getComponent(index) == component)
                return index;
        }/* w ww.j a  v  a2s  .c o m*/
    }
    return -1;
}

From source file:Main.java

public static void revalidateContainer(Component comp) {
    Container container = comp.getParent();
    if (container instanceof JComponent) {
        ((JComponent) container).revalidate();
    }/*w  w w .j  av a2s. co m*/
}

From source file:Main.java

public static void getComponentTreePosition(Component component, ArrayList position) {
    if (component.getParent() == null) {
        return;/*from w w w  .  j  av a 2  s.c o m*/
    }
    getComponentTreePosition(component.getParent(), position);
    position.add(new Integer(component.getParent().getComponentCount() - getComponentIndex(component)));
}

From source file:Main.java

/**
 * @return the visible size of a component in its viewport (including
 *         scrollbars)/* ww w.j a v  a2  s. c o  m*/
 */
public static Dimension getVisibleSizeInViewport(Component c) {
    if (c.getParent() instanceof JViewport) {
        JViewport vp = (JViewport) c.getParent();
        Dimension d = vp.getExtentSize();
        if (vp.getParent() instanceof JScrollPane) {
            JScrollPane sp = (JScrollPane) vp.getParent();
            if (sp.getVerticalScrollBar() != null && sp.getVerticalScrollBar().isVisible()) {
                d.width += sp.getVerticalScrollBar().getWidth();
            }
            if (sp.getHorizontalScrollBar() != null && sp.getHorizontalScrollBar().isVisible()) {
                d.height += sp.getHorizontalScrollBar().getHeight();
            }
        }
        return d;
    } else {
        return null;
    }
}

From source file:Main.java

public static final int getComponentIndex(final Component component) {
    if (component != null && component.getParent() != null) {
        final Container c = component.getParent();
        for (int i = 0; i < c.getComponentCount(); i++) {
            if (c.getComponent(i) == component) {
                return i;
            }//from   w  w  w  .j  a v  a  2s .c  o m
        }
    }
    return -1;
}

From source file:Main.java

/**
 * Gets the scroll pane around the component.
 *
 * @param innerComponent//from w ww .j av  a 2s. c  o  m
 * @return the scroll pane. Null if the component is not in any JScrollPane.
 */
public static Component getScrollPane(Component innerComponent) {
    Component component = innerComponent;

    if ((component.getParent() != null) && (component.getParent().getParent() != null)
            && (component.getParent().getParent() instanceof JScrollPane)) {
        component = (JComponent) component.getParent().getParent();

        return component;

    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Gets the top parent Window of the given component. There is a limit to how far up the parent stack it'll go.
 *
 * @param <T>//from  w  w  w  .j a  v a2  s  .com
 * @param jComponent
 * @return
 */
public static <T extends Window> T getParentWindow(Component jComponent) {
    Container parent = jComponent.getParent();
    int i = 0;
    int limit = 100;
    while (true) {
        parent = parent.getParent();
        if (parent instanceof Window) {
            return (T) parent;
        }
        i++;
        if (i > limit) {
            return null; //Just to prevent an infinite loop...
        }
    }
}

From source file:Main.java

/**
 * Finds the top level parent (frame/dialog/window) of any component
 * @param component Component//from   ww w  . ja  v a 2 s .c om
 * @return Parent or null if none found
 */
public static Component getTopLevelParent(Object component) {
    Component top = null;

    if (component instanceof Component) {
        Component c = (Component) component;

        while (c.getParent() != null) {
            c = c.getParent();
        }
        top = c;
    }

    return top;
}

From source file:Main.java

/**
 * Centers a component on its parent./*from w  w w. ja v a2s  . c  o  m*/
 *
 * @param component
 */
public static void centerInParent(Component component) {
    Container parent = component.getParent();
    if (parent != null) {
        Rectangle parentBounds = parent.getBounds();
        Rectangle dialogBounds = new Rectangle(
                (int) (parentBounds.getMinX() + parentBounds.getWidth() / 2 - component.getWidth() / 2),
                (int) (parentBounds.getMinY() + parentBounds.getHeight() / 2 - component.getHeight() / 2),
                component.getWidth(), component.getHeight());
        //dialog.setBounds( dialogBounds );
        component.setLocation(dialogBounds.x, dialogBounds.y);
    }
}

From source file:Main.java

public static <T extends Component> T findParentComponentOfType(Component component, Class<T> type) {
    do {/* w w w. j av a2s.  c om*/
        component = component.getParent();
        if (component != null && type.isAssignableFrom(component.getClass())) {
            return (T) component;
        }
    } while (component != null);

    return null;
}