Java JComponent Size getPreferredSize(Window window)

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

Description

Gets the preferred size of the given Window .

License

Open Source License

Declaration

public static Dimension getPreferredSize(Window window) 

Method Source Code


//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;
    }
}

Related

  1. getLocalScreenSize(Component invoker)
  2. getMaxSize(JComponent[] components)
  3. getOptimalScreenSize(Container container, Dimension currentDim)
  4. getPreferredSize(JComponent component, int width)
  5. getPreferredSize(String html, boolean width, int prefSize)
  6. getPreferredSizeComponent(JComponent component)
  7. getScreenSize()
  8. getScreenSize(Component invoker)
  9. getSize(byte[] data)