Java Screen Full Size ensureWindowOnScreen(Window window)

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

Description

Ensures that specified window do not exceed screen size, including possible task bar.

License

Open Source License

Parameter

Parameter Description
window The window to be placed exactly on screen.

Declaration

public static void ensureWindowOnScreen(Window window) 

Method Source Code

//package com.java2s;
/*/*from  ww w.  j  a v  a 2s . co m*/
 *   Swing Explorer. Tool for developers exploring Java/Swing-based application internals. 
 *     Copyright (C) 2012, Maxim Zakharenkov
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library 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
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *   
 */

import java.awt.Dimension;

import java.awt.GraphicsConfiguration;

import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Window;

public class Main {
    /**
     * Ensures that specified window do not exceed screen size, including
     * possible task bar.
     * @param window The window to be placed exactly on screen.
     */
    public static void ensureWindowOnScreen(Window window) {
        Toolkit kit = Toolkit.getDefaultToolkit();
        GraphicsConfiguration gc = window.getGraphicsConfiguration();
        Insets ins = kit.getScreenInsets(gc);
        Dimension totalSize = kit.getScreenSize();

        int availWidth = totalSize.width - ins.left - ins.right;
        int availHeight = totalSize.height - ins.top - ins.bottom;

        // modify location and size only if window is out of screen:
        Point loc = window.getLocation();
        Dimension size = window.getSize();
        if (loc.x < ins.left || loc.y < ins.top || loc.x + size.width > availWidth
                || loc.y + size.height > availHeight) {

            // modify size to be inside the screen:
            if (size.width > availWidth) {
                size.width = availWidth;
            }
            if (size.height > availHeight) {
                size.height = availHeight;
            }
            window.setSize(size);

            // modify locaiton to be on screen:
            if (loc.x < ins.left) {
                loc.x = ins.left;
            }
            if (loc.y < ins.top) {
                loc.y = ins.top;
            }

            if (loc.x + size.width > totalSize.width - ins.right) {
                loc.x = totalSize.width - ins.right - size.width;
            }
            if (loc.y + size.height > totalSize.height - ins.bottom) {
                loc.y = totalSize.height - ins.bottom - size.height;
            }
            window.setLocation(loc);
        }
    }
}

Related

  1. bringWindowToScreen(Window window)
  2. centreInScreen(Container containee, int xOffSet, int yOffSet)
  3. centrePositionOnScreen(Window window)
  4. ensureOnScreen(Container container)
  5. factorScreenDimension(Window window, double widthFactor, double heightFactor)
  6. fitInScreen(Component comp)
  7. fitInScreen(Window o)
  8. fitToScreen(Dimension size, GraphicsConfiguration screen)