Java Screen Center screenCenter(Window window)

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

Description

Center screen

License

Apache License

Declaration

public static void screenCenter(Window window) 

Method Source Code


//package com.java2s;
/*/* w ww  .jav  a  2s.c  om*/
 * Created on 01.02.2005
 *
 * JDBReport Generator
 * 
 * Copyright (C) 2005-2014 Andrey Kholmanskih
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

import java.awt.*;

public class Main {
    /**
     * Center screen
     */
    public static void screenCenter(Window window) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = window.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        window.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    }

    public static void screenCenter(Window window, Window parent) {
        Dimension screenSize;
        Point location;
        if (parent == null) {
            screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            location = new Point(0, 0);
        } else {
            screenSize = parent.getSize();
            location = parent.getLocation();
        }
        Dimension frameSize = window.getSize();
        window.setLocation(location.x + (screenSize.width - frameSize.width) / 2,
                location.y + (screenSize.height - frameSize.height) / 2);
    }
}

Related

  1. locateOnScreenCenter(Component component)
  2. moveToCenter(Component componentToMove, Component componentToCenterOver)
  3. moveToScreenCenter(Window w)
  4. packAndCenter(Window wind)
  5. packAndCenter(Window window)
  6. setCenter(Component comp)
  7. setCenter(Component comp, Component parent)
  8. setCenter(Component component)
  9. setCenter(Component component, Component component1)