Java tutorial
//package com.java2s; import java.awt.Container; public class Main { static public <T extends Container> T findAncestorOfType(Container acomp, Class<T> type) { Container parent = acomp; while (parent != null) { if (type.isInstance(parent)) return type.cast(parent); parent = parent.getParent(); } return null; } }