Returns the parent JFrame of the specified component. - Java Swing

Java examples for Swing:JFrame

Description

Returns the parent JFrame of the specified component.

Demo Code


//package com.java2s;

import java.awt.Component;
import java.awt.Container;

import javax.swing.JFrame;

public class Main {
    /**//from w w w. j a v a  2  s.c  o m
     * Returns the parent <code>JFrame</code> of the specified component.
     * 
     * @param component the component to find the parent JFrame for.
     * @return the parent JFrame.
     */
    public static JFrame getParentJFrame(Component component) {
        if ((component == null) || (component instanceof JFrame)) {
            return (JFrame) component;
        }
        Container c = component.getParent();
        while ((c != null) && !(c instanceof JFrame)) {
            c = c.getParent();
        }
        return (JFrame) c;
    }
}

Related Tutorials