Here you can find the source of growIfNecessary(Window window)
public static void growIfNecessary(Window window)
//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; } }