Here you can find the source of centerDialog(JDialog dialog, JFrame frame)
Parameter | Description |
---|---|
dialog | the dialog to center |
frame | the frame |
Parameter | Description |
---|---|
NullPointerException | if either dialog or frameare null |
public static void centerDialog(JDialog dialog, JFrame frame) throws NullPointerException
//package com.java2s; /*// w w w.j a v a 2 s .c om * ..::jDrawingLib::.. * * Copyright (C) Federico Vera 2012 - 2014 <dktcoding [at] gmail> * * This program 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 any later * version. * * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */ import java.awt.Dimension; import javax.swing.JDialog; import javax.swing.JFrame; public class Main { /** * Centers a dialog in a frame * * @param dialog the dialog to center * @param frame the frame * @throws NullPointerException if either {@code dialog} or {@code frame} * are {@code null} */ public static void centerDialog(JDialog dialog, JFrame frame) throws NullPointerException { if (dialog == null) { throw new NullPointerException("The dialog can't be null"); } if (frame == null) { throw new NullPointerException("The frame can't be null"); } final Dimension d = dialog.getSize(); // frame.setLocationByPlatform(true); dialog.setLocationRelativeTo(frame); dialog.setLocation((frame.getWidth() - d.width) / 2, (frame.getHeight() - d.height) / 2); } }