Here you can find the source of getPreferredSize(Window window)
public static Dimension getPreferredSize(Window window)
//package com.java2s; /*//w ww.j av a2 s . c o 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 { /** * 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; } }