Java Window growIfNecessary(Window window)

Here you can find the source of growIfNecessary(Window window)

Description

Grows the given Window if its preferred size exceeds its actual size.

License

Open Source License

Declaration

public static void growIfNecessary(Window window) 

Method Source Code


//package com.java2s;
/*//ww w .j  av  a 2 s  .  co  m
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

import java.awt.*;

import javax.swing.*;

public class Main {
    /**
     * Grows the given {@code Window} if its preferred size exceeds its actual
     * size.  Will not shrink the {@code Window}.
     */
    public static void growIfNecessary(Window window) {
        Dimension size = window.getSize();
        Dimension pSize = getPreferredSize(window);

        window.setSize(Math.max(size.width, pSize.width), Math.max(size.height, pSize.height));
    }

    /**
     * Gets the preferred size of the given {@code Window}.  If the given {@code
     * Window} is a {@code JFrame} or a {@code JDialog}, and its glass pane is
     * visible, the glass pane's preferred size will be accounted for as well
     * ({@code Window.getPreferredSize} does not take the glass pane into
     * consideration).
     */
    public static Dimension getPreferredSize(Window window) {
        Dimension pSize = window.getPreferredSize();

        Component glass = null;
        Component content = null;

        if (window instanceof JFrame) {
            JFrame frame = (JFrame) window;
            glass = frame.getGlassPane();
            content = frame.getContentPane();
        } else if (window instanceof JDialog) {
            JDialog dialog = (JDialog) window;
            glass = dialog.getGlassPane();
            content = dialog.getContentPane();
        }

        if (glass != null && glass.isVisible()) {
            Dimension cpSize = content.getPreferredSize();
            content.setPreferredSize(glass.getPreferredSize());

            Dimension newPSize = window.getPreferredSize();

            pSize.width = Math.max(pSize.width, newPSize.width);
            pSize.height = Math.max(pSize.height, newPSize.height);

            // Restore previous preferred size, if any
            content.setPreferredSize(cpSize);
        }

        return pSize;
    }
}

Related

  1. getWindow(final Object source)
  2. getWindow(String title)
  3. getWindowNormalBounds(Window window)
  4. getWindowTitle(Window window)
  5. getWindowTitle(Window window)
  6. hideWaitPane(Window win, Timer timer)
  7. installPrefsHandler(final Preferences prefs, final String name, Window window)
  8. isResizable(Window window)
  9. modifyDimensions(Window window)