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 parent for the given component. Other than Component#getParent(), this method will work for popup
 * menus as well, returning the menu's invoker rather than its own window instance.
 *
 * @param c Component to query//from w w  w.  j av a 2s.  c o  m
 * @return Parent element of the given component.
 */
public static synchronized Component getParent(final Component c) {
    if (c instanceof JPopupMenu)
        return ((JPopupMenu) c).getInvoker();
    else
        return c.getParent();
}

From source file:Main.java

/**
 * Gets the root pane of the given component.
 * //ww w  . j  a  va2  s .  c  o  m
 * @param    component      The component whose root pane is retrieved.
 * @return                The root pane of the component.
 */
public static JRootPane getRootPane(Component component) {

    if (component instanceof JRootPane) {
        return (JRootPane) component;
    }
    if (component.getParent() != null) {
        return getRootPane(component.getParent());
    }

    // Get the window of the component.
    Window window = SwingUtilities.windowForComponent(component);
    return getRootPane(window);

}

From source file:Main.java

private static Component getParent(Component component) {
    if (component instanceof JPopupMenu) {
        component = ((JPopupMenu) component).getInvoker();
    } else {/* w w  w  .j av  a2 s . c  o  m*/
        component = component.getParent();
    }
    return component;
}

From source file:Main.java

public static void addMiddleButtonDragSupport(Component targetComponent) {
    MouseInputAdapter mia = new MouseInputAdapter() {
        int m_XDifference, m_YDifference;
        boolean m_dragging = false;

        public void mouseDragged(MouseEvent e) {
            if (!m_dragging)
                return;

            Component target = e.getComponent();
            Container c = target.getParent();
            if (c instanceof JViewport) {
                JViewport jv = (JViewport) c;
                Point p = jv.getViewPosition();
                int newX = p.x - (e.getX() - m_XDifference);
                int newY = p.y - (e.getY() - m_YDifference);

                int maxX = target.getWidth() - jv.getWidth();
                int maxY = target.getHeight() - jv.getHeight();
                if (newX < 0)
                    newX = 0;// w  w  w  . ja  va2  s .  co  m
                if (newX > maxX)
                    newX = maxX;
                if (newY < 0)
                    newY = 0;
                if (newY > maxY)
                    newY = maxY;

                jv.setViewPosition(new Point(newX, newY));
            }
        }

        Cursor oldCursor;

        public void mousePressed(MouseEvent e) {
            if (SwingUtilities.isMiddleMouseButton(e)) {
                m_dragging = true;
                oldCursor = e.getComponent().getCursor();
                e.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
                m_XDifference = e.getX();
                m_YDifference = e.getY();
            }
        }

        public void mouseReleased(MouseEvent e) {
            if (m_dragging) {
                e.getComponent().setCursor(oldCursor);
                m_dragging = false;
            }
        }
    };
    targetComponent.addMouseMotionListener(mia);
    targetComponent.addMouseListener(mia);
}

From source file:Main.java

/**
 * Convenience method for searching above the given component in the component
 * hierarchy and returns the first object of the given type it finds, or
 * <code>null</code> if no such parent was found.
 * <p>/*  ww w.ja  v a 2  s. c  om*/
 * The reason this method exists is for tidyness of the calling code, as no
 * longer a explicit cast is needed.
 * 
 * @param aType
 *          the type of the parent to find, cannot be <code>null</code>;
 * @param aComponent
 *          the component to search in the hierarchy, cannot be
 *          <code>null</code>.
 * @return the requested ancestor, or <code>null</code> if not found.
 * @see SwingUtilities#getAncestorOfClass(Class, Component)
 */
@SuppressWarnings("unchecked")
public static <T> T getAncestorOfClass(final Class<T> aType, final Component aComponent) {
    if ((aComponent == null) || (aType == null)) {
        return null;
    }

    Container parent = aComponent.getParent();
    while ((parent != null) && !(aType.isInstance(parent))) {
        parent = parent.getParent();
    }

    return (T) parent;
}

From source file:Main.java

public static void scrollRectToVisible(Component component, Rectangle aRect) {
    Container parent;/*from   w  w w .  j a va 2  s  .c om*/
    int dx = component.getX(), dy = component.getY();

    for (parent = component.getParent(); parent != null
            && (!(parent instanceof JViewport) || (((JViewport) parent)
                    .getClientProperty("HierarchicalTable.mainViewport") == null)); parent = parent
                            .getParent()) {
        Rectangle bounds = parent.getBounds();

        dx += bounds.x;
        dy += bounds.y;
    }

    if (parent != null) {
        aRect.x += dx;
        aRect.y += dy;

        ((JComponent) parent).scrollRectToVisible(aRect);
        aRect.x -= dx;
        aRect.y -= dy;
    }
}

From source file:Main.java

/**
 * Returns the parent window for the component by navigating up the
 * component hierarchy. /*from w  w  w.  j  a  v a2  s .c  om*/
 *
 * @param comp the component to test.
 * @return the parent {@link Window} if one is found.
 * @throws HeadlessException
 */
public static Window getWindowForComponent(Component comp) throws HeadlessException {
    if (comp instanceof Frame || comp instanceof Dialog) {
        return (Window) comp;
    }
    return getWindowForComponent(comp.getParent());
}

From source file:Main.java

public static Window getWindowAncestor(Component component) {
    while (component instanceof Window == false) {
        if (component == null) {
            return null;
        }/* w ww. ja va  2s. c  o m*/
        component = component.getParent();
    }
    return (Window) component;
}

From source file:Main.java

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

From source file:Main.java

/**
 * Finds the nearest RootPaneContainer of the provided Component. 
 * Primarily, if a JPopupMenu (such as used by JMenus when they are visible) has no parent,
 * the search continues with the JPopupMenu's invoker instead. Fixes BSAF-77
 *
 * @return a RootPaneContainer for the provided component
 * @param root the Component/*from   w  w  w  . j  av  a 2  s  .co  m*/
 *
 * @author Eric Heumann
 * @since 1.9
 */
public static RootPaneContainer findRootPaneContainer(Component root) {
    while (root != null) {
        if (root instanceof RootPaneContainer) {
            return (RootPaneContainer) root;
        } else if (root instanceof JPopupMenu && root.getParent() == null) {
            root = ((JPopupMenu) root).getInvoker();
        } else {
            root = root.getParent();
        }
    }
    return null;
}