Java JFrame Center centerFrame(JFrame frame, boolean isPopup)

Here you can find the source of centerFrame(JFrame frame, boolean isPopup)

Description

Centers a JFrame to the screen.

License

Open Source License

Parameter

Parameter Description
frame JFrame frame to center
isPopup boolean is the frame a popup dialog?

Declaration

public static void centerFrame(JFrame frame, boolean isPopup) 

Method Source Code

//package com.java2s;
/*//from  w  w  w  . j av  a2 s  .  co m
 * CoreUtility.java
 * Copyright 2002-2003 (C) B. K. Oxley (binkley)
 * <binkley@alumni.rice.edu>
 *
 * 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
 *
 * Created on Februrary 4th, 2002.
 */

import java.awt.Dimension;

import java.awt.GraphicsEnvironment;

import java.awt.Rectangle;

import javax.swing.JFrame;

public class Main {
    /**
     * Centers a <code>JFrame</code> to the screen.
     *
     * @param frame JFrame frame to center
     * @param isPopup boolean is the frame a popup dialog?
     */
    public static void centerFrame(JFrame frame, boolean isPopup) {
        // since the Toolkit.getScreenSize() method is broken in the Linux implementation
        // of Java 5  (it returns double the screen size under xinerama), this method is
        // encapsulated to accomodate this with a hack.
        // TODO: remove the hack, once Java fixed this.
        // final Dimension screenSize = getScreenSize(Toolkit.getDefaultToolkit());
        final Rectangle screenSize = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                .getDefaultConfiguration().getBounds();

        if (isPopup) {
            frame.setSize(screenSize.width / 2, screenSize.height / 2);
        }

        final Dimension frameSize = frame.getSize();

        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }

        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }

        frame.setLocation(screenSize.x + (screenSize.width - frameSize.width) / 2,
                screenSize.y + (screenSize.height - frameSize.height) / 2);
    }
}

Related

  1. centerFrame(JFrame frame)
  2. centerFrame(JFrame frame)
  3. centerFrame(JFrame frame)
  4. centerFrame(JFrame frame)
  5. centerFrame(JFrame frame)
  6. centerFrame(Window owner)
  7. centerFrameOnMainDisplay(final JFrame frame)
  8. centerFrameOnScreen(JFrame frame)
  9. centerFrameOnScreen(JFrame frame)