Java JComponent Container getTopAncestor(JComponent start)

Here you can find the source of getTopAncestor(JComponent start)

Description

search of the hierarcy of components for an instance of a given class

License

Apache License

Parameter

Parameter Description
start - starting component
theClass - required class

Return

possibly null component - null says no ancestor found

Declaration

public static Container getTopAncestor(JComponent start) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import javax.swing.*;

import java.awt.*;

public class Main {
    public static Component getTopAncestor(Component c) {
        Component ret = c.getParent();
        if (ret == null)
            return c;
        return getTopAncestor(ret);
    }//from  w  w  w .j a  v  a 2 s.  c om

    /**
     * search of the hierarcy of components for an instance of a given class
     *
     * @param start    - starting component
     * @param theClass - required class
     * @return possibly null component - null says no ancestor found
     */
    public static Container getTopAncestor(JComponent start) {
        Container parent = start.getParent();
        if (parent == null)
            return start;
        if (parent instanceof JComponent)
            return getTopAncestor((JComponent) parent);
        if (parent instanceof JFrame)
            return parent;
        if (parent instanceof JDialog)
            return parent;
        if (parent instanceof JWindow)
            return parent;
        else
            return null;
    }
}

Related

  1. getRootPane(final Component component)
  2. getRootPaneContainer(Component c)
  3. getRootPaneContainer(Component c)
  4. getRootWindow(Component component)
  5. getStringValue(JComponent component)
  6. getTopLevelContainer(JComponent component)
  7. getWindow(final JComponent comp)
  8. getWindow(JComponent c)
  9. getWindowLocation(JComponent start)