Java JFrame Parent getFrame(Component parent)

Here you can find the source of getFrame(Component parent)

Description

Find the parent Frame.

License

Open Source License

Parameter

Parameter Description
parent a component to find the frame of.

Return

The parent Frame

Declaration

public static Frame getFrame(Component parent) 

Method Source Code

//package com.java2s;
/**/*  www.java  2  s .c  o m*/
 * Distribution License:
 * JSword is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License, version 2.1 as published by
 * the Free Software Foundation. This program is distributed in the hope
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * The License is available on the internet at:
 *       http://www.gnu.org/copyleft/lgpl.html
 * or by writing to:
 *      Free Software Foundation, Inc.
 *      59 Temple Place - Suite 330
 *      Boston, MA 02111-1307, USA
 *
 * Copyright: 2005
 *     The copyright to this program is held by it's authors.
 *
 * ID: $Id: GuiUtil.java 2045 2010-12-06 21:19:49Z dmsmith $
 */

import java.awt.Component;

import java.awt.Frame;

import javax.swing.JOptionPane;

public class Main {
    /**
     * Find the parent Frame. This method can do more than simply walking up the
     * tree to find a parent frame by looking for default frames from
     * JOptionPane and by looking for all visible Frames. We can be sure to
     * return a Frame from this method even if null is passed in.
     * 
     * @param parent
     *            a component to find the frame of.
     * @return The parent Frame
     */
    public static Frame getFrame(Component parent) {
        if (parent == null) {
            // So we can't get one by walking up the tree so we will have to
            // get one from somewhere else.
            // Firstly someone might have called JOptionPane.setRootFrame()
            // to give us a reasonable default so try there
            Frame option = JOptionPane.getRootFrame();

            // If a default has not been set, JOptionPane.getRootFrame() calls
            // SwingUtilities.getSharedOwnerFrame() to get a new invisible frame
            // and we may be able to do better than that. Unfortunately the
            // getSharedOwnerFrame() method is not public so we have to trick
            // our way to finding if we got a duff default
            if (!option.getClass().getName().startsWith("javax.swing.SwingUtilities$")) {
                // So we think the JOptionPane root frame is our creation
                return option;
            }

            // We might be able to get a better default by looking through all
            // the frames and picking the biggest visible one.
            Frame best = null;
            int bestSize = 0;

            Frame[] frames = Frame.getFrames();
            for (int i = 0; i < frames.length; i++) {
                Frame frame = frames[i];
                if (frame.isVisible()) {
                    // So this frame is a candidate
                    int thisSize = frame.getWidth() * frame.getHeight();
                    if (best == null || thisSize > bestSize) {
                        best = frame;
                        bestSize = thisSize;
                    }
                }
            }

            // So if we found a frame from searching then use that
            if (best != null) {
                return best;
            }

            // if all else fails we will have to use the invisible frame
            // provided by JOptionPane
            return option;
        }

        if (parent instanceof Frame) {
            return (Frame) parent;
        }

        // So we walk up the tree looking for a frame
        return getFrame(parent.getParent());
    }

    /**
     * Find the best frame to which to root a dialog, generally the largest
     * visible frame of the application.
     * 
     * @return the best frame.
     */
    public static Frame getRootFrame() {
        return getFrame(null);
    }
}

Related

  1. ensureVisibilityAtParent(final JInternalFrame frame)
  2. execLoop(JComponent editor, Frame parent, boolean modal, int w, int h)
  3. fileOpen(Frame parent, String typename, String ext)
  4. findParentDialogOrFrame(Container container)
  5. getFirstParentFrameOrDialog(Component cmp)
  6. getFrameParent(Component component)
  7. getInstalledOperation(final RootPaneContainer frame, final Object actionKey, boolean selfOnly)
  8. getParentalFrame(Component n)
  9. getParentFrame(Component component)