Java JComponent Size getScreenSize()

Here you can find the source of getScreenSize()

Description

Returns the maximum possible size of a fully visible window.

License

Open Source License

Declaration

public static java.awt.Dimension getScreenSize() 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2006-2013 AITIA International, Inc.
 * //from   www.ja  v  a 2  s  .c om
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

public class Main {
    private static java.awt.Component g_referenceComponent = null;
    private static java.awt.Dimension g_screenSize = null;

    /**
     * Returns the maximum possible size of a fully visible window.
     * Note that this method is slow and expensive for the first time.
     */
    public static java.awt.Dimension getScreenSize() {
        if (g_screenSize == null) {
            java.awt.Component c = getRefComp();
            java.awt.Toolkit tk = c.getToolkit();
            g_screenSize = tk.getScreenSize();
            try {
                java.awt.Insets in = tk.getScreenInsets(c.getGraphicsConfiguration());
                g_screenSize.width -= in.left + in.right;
                g_screenSize.height -= in.top + in.bottom;
            } catch (Throwable t) {
            }
            //java.awt.Rectangle rect = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
            //g_screenSize = rect.getSize();
            //System.gc();
        }
        return g_screenSize;
    }

    /** 
     * Some functions (such as font/screen size calculation) require a component
      * e.g. to access the screen. I call it as 'reference component'.
      * It is recommended to set it to a JLabel/JButton in the main window.
      */
    public static java.awt.Component getRefComp() {
        if (g_referenceComponent == null) {
            g_referenceComponent = java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
            if (g_referenceComponent == null)
                g_referenceComponent = new javax.swing.JLabel();
        }
        return g_referenceComponent;
    }
}

Related

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