Java JFrame Center centerDialog(JDialog dialog, JFrame frame)

Here you can find the source of centerDialog(JDialog dialog, JFrame frame)

Description

Centers a dialog in a frame

License

Open Source License

Parameter

Parameter Description
dialog the dialog to center
frame the frame

Exception

Parameter Description
NullPointerException if either dialog or frameare null

Declaration

public static void centerDialog(JDialog dialog, JFrame frame) throws NullPointerException 

Method Source Code


//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);
    }
}

Related

  1. center(JFrame frame)
  2. center(JFrame parent, JInternalFrame dialog)
  3. centerAndSizeFrame(JFrame frame)
  4. centerBigFrame(JFrame frame, int maxWidth, int maxHeight, double scaling, int minHeight)
  5. centerDialog(final JFrame frame, JDialog dialog)
  6. centerDialog(JDialog dialog, JFrame parent)
  7. centerDialogIntoFrame(java.awt.Component p_CompToBePositioned, javax.swing.JFrame p_MainFrame)
  8. centerDialogOnFrame(JFrame parentFrame, JDialog dialog)
  9. centerFrame(final JFrame target)