Here you can find the source of centerDialog(JDialog dialog)
JDialog
to the screen.
Parameter | Description |
---|---|
dialog | JDialog dialog to center |
public static void centerDialog(JDialog dialog)
//package com.java2s; /*//w w w . ja v a 2s .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.JDialog; public class Main { /** * Centers a <code>JDialog</code> to the screen. * * @param dialog JDialog dialog to center */ public static void centerDialog(JDialog dialog) { // 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 = Toolkit.getDefaultToolkit().getScreenSize(); final Rectangle screenSize = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() .getDefaultConfiguration().getBounds(); final Dimension dialogSize = dialog.getSize(); if (dialogSize.height > screenSize.height) { dialogSize.height = screenSize.height; } if (dialogSize.width > screenSize.width) { dialogSize.width = screenSize.width; } dialog.setLocation(screenSize.x + (screenSize.width - dialogSize.width) / 2, screenSize.y + (screenSize.height - dialogSize.height) / 2); dialog.setSize(dialogSize); } }