Java JComponent Container getAncestorsOfClass(JComponent start, Class theClass)

Here you can find the source of getAncestorsOfClass(JComponent start, Class theClass)

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

non-null array of all matching ancestors of type theClass

Declaration

public static Object[] getAncestorsOfClass(JComponent start, Class theClass) 

Method Source Code


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

import javax.swing.*;

import java.awt.*;

import java.lang.reflect.Array;

import java.util.*;
import java.util.List;

public class Main {
    /**/*from  ww w . java 2  s.c  o  m*/
     * search of the hierarcy of components for an instance of a given class
     *
     * @param start    - starting component
     * @param theClass - required class
     * @return non-null array of all matching ancestors of type
     *         theClass
     */
    public static Object[] getAncestorsOfClass(JComponent start, Class theClass) {
        List holder = new ArrayList();
        Container parent = start.getParent();
        while (parent != null) {
            if (theClass.isInstance(parent))
                holder.add(parent);
            parent = parent.getParent();
        }
        Object[] ret = (Object[]) Array.newInstance(theClass, holder.size());
        holder.toArray(ret);
        return ret;
    }
}

Related

  1. findRootPaneContainer(Component root)
  2. findRootPaneContainer(final Component source)
  3. getActiveRectangle(JComponent c)
  4. getAllJComponents(Container container, Collection collection)
  5. getAncestorOfType(JComponent component, Class type)
  6. getBooleanClientProperty(JComponent c, Object property)
  7. getChildJComponents(Container container)
  8. getClipboard(JComponent c)
  9. getComponent(JComponent container, String name)