Gets the root pane of the given component. - Java Swing

Java examples for Swing:JComponent

Description

Gets the root pane of the given component.

Demo Code


//package com.java2s;
import java.awt.Component;

import java.awt.Window;

import javax.swing.JDialog;
import javax.swing.JFrame;

import javax.swing.JRootPane;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;

public class Main {
    /**//  w ww.  j a  v  a 2  s. c om
     * Gets the root pane of the given component.
     * 
     * @param    component      The component whose root pane is retrieved.
     * @return                The root pane of the component.
     */
    public static JRootPane getRootPane(Component component) {

        if (component instanceof JRootPane) {
            return (JRootPane) component;
        }
        if (component.getParent() != null) {
            return getRootPane(component.getParent());
        }

        // Get the window of the component.
        Window window = SwingUtilities.windowForComponent(component);
        return getRootPane(window);

    }

    /**
     * Gets the root pane of the window.
     * The window should be a javax.swing.JFrame, javax.swing.JDialog
     * or javax.swing.JWindow. Otherwise null is returned.
     * 
     * @param    window         The window whose root pane is retrieved.
     * @return                The root pane of the window of the component.
     */
    public static JRootPane getRootPane(Window window) {

        if (window == null) {
            return null;
        }

        // Get the root pane if we can find one.
        if (window instanceof JFrame)
            return ((JFrame) window).getRootPane();
        if (window instanceof JWindow)
            return ((JWindow) window).getRootPane();
        if (window instanceof JDialog)
            return ((JDialog) window).getRootPane();

        // We could not find a root pane for this window.
        return null;

    }
}

Related Tutorials