get Component Top level Ancestor - Java java.awt

Java examples for java.awt:Component

Description

get Component Top level Ancestor

Demo Code


//package com.java2s;
import javax.swing.*;
import java.applet.Applet;
import java.awt.*;

public class Main {
    public static Component getToplevelAncestor(Component component) {
        for (Component p = component; p != null; p = p.getParent()) {
            if (p instanceof Window || p instanceof Applet) {
                return p;
            } else if (p instanceof JPopupMenu) {
                return getToplevelAncestor(((JPopupMenu) p).getInvoker());
            }/*from   w w  w  .ja  va2s. c  o  m*/
        }
        return null;
    }
}

Related Tutorials