Here you can find the source of getAncestorsOfClass(JComponent start, Class theClass)
Parameter | Description |
---|---|
start | - starting component |
theClass | - required class |
public static Object[] getAncestorsOfClass(JComponent start, Class theClass)
//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; } }