Java JFrame Center centerWindow(JFrame frame)

Here you can find the source of centerWindow(JFrame frame)

Description

Move frame to center of current screen

License

Open Source License

Parameter

Parameter Description
frame frame to centering

Declaration

public static void centerWindow(JFrame frame) 

Method Source Code


//package com.java2s;
/*//from  www .j a  v a  2  s . com
 * Collab desktop - Software for shared drawing via internet in real-time
 * Copyright (C) 2012 Martin Indra <aktive@seznam.cz>
 *
 * This file is part of Collab desktop.
 *
 * Collab desktop 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.
 *
 * Collab desktop 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 Collab desktop.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.GraphicsEnvironment;
import java.awt.Point;
import javax.swing.JFrame;

public class Main {
    /**
     * Move frame to center of current screen
     * 
     * @param frame frame to centering
     */
    public static void centerWindow(JFrame frame) {
        Point centerPoint = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
        frame.setLocation(Math.max(0, centerPoint.x - (frame.getWidth() / 2)),
                Math.max(0, centerPoint.y - (frame.getHeight() / 2)));
    }
}

Related

  1. centerOnParent(JDialog child, JFrame parent)
  2. centerOnScreen(JFrame frame, int screenID)
  3. centerPoint(int width, int height, JFrame frame)
  4. centerPosition(JFrame invokerFrame, Window w, int width, int height)
  5. centerWindow(JFrame frame)
  6. centerWindow(JFrame frame)
  7. centerWindow(JFrame window)
  8. centerWindowInFrame(Component window, Window frame)
  9. centerWindowOnParent(Window w, JFrame parent)