Here you can find the source of screenCenter(Window window)
public static void screenCenter(Window window)
//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); } }