Java JFrame Center centerOnParent(JDialog child, JFrame parent)

Here you can find the source of centerOnParent(JDialog child, JFrame parent)

Description

Centers a Dialog on its parent frame;

License

Open Source License

Parameter

Parameter Description
child a parameter
parent a parameter

Declaration

public static void centerOnParent(JDialog child, JFrame parent) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2010 Olaf Sebelin/* w  w  w. ja  v  a2  s.co  m*/
 * 
 * This file is part of Verdandi.
 * 
 * Verdandi 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.
 * 
 * Verdandi 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 Verdandi.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import javax.swing.JDialog;
import javax.swing.JFrame;

public class Main {
    /**
     * Centers a Dialog on its parent frame;
     * 
     * @param child
     * @param parent
     */
    public static void centerOnParent(JDialog child, JFrame parent) {

        int posX = parent.getX() + (parent.getWidth() / 2) - (child.getWidth() / 2);
        int posY = parent.getY() + (parent.getHeight() / 2) - (child.getHeight() / 2);
        child.setBounds(posX, posY, child.getWidth(), child.getHeight());
    }
}

Related

  1. centerFrameOnScreen(JFrame frame)
  2. centerFrameTo(final JFrame frameToCenter, final Dimension orientDim)
  3. centerJFrame(JFrame frame)
  4. centerOnFrame(JDialog dialog, JFrame frame)
  5. centerOnFrame(JFrame frame, Component comp)
  6. centerOnScreen(JFrame frame, int screenID)
  7. centerPoint(int width, int height, JFrame frame)
  8. centerPosition(JFrame invokerFrame, Window w, int width, int height)
  9. centerWindow(JFrame frame)