Finds the top-level JFrame in the component tree containing a given component. - Java Swing

Java examples for Swing:JFrame

Description

Finds the top-level JFrame in the component tree containing a given component.

Demo Code


//package com.java2s;

import java.awt.Component;

import javax.swing.JFrame;

public class Main {
    /**/*from w w  w  .jav  a  2  s  .c  o  m*/
     * Finds the top-level JFrame in the component tree containing a given component.
     * 
     * @param comp leaf component to search up from
     * @return the containing JFrame or null if none
     */
    public static JFrame getTopFrame(Component comp) {
        if (comp == null)
            return null;
        while (comp.getParent() != null)
            comp = comp.getParent();
        if (comp instanceof JFrame)
            return (JFrame) comp;
        return null;
    }
}

Related Tutorials