Java JFrame Center centerDialogOnFrame(JFrame parentFrame, JDialog dialog)

Here you can find the source of centerDialogOnFrame(JFrame parentFrame, JDialog dialog)

Description

Center the dialog on the parent frame.

License

Apache License

Parameter

Parameter Description
parentFrame the parent frame
dialog the dialog

Declaration

public static void centerDialogOnFrame(JFrame parentFrame, JDialog dialog) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.Dimension;

import java.awt.Point;

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

public class Main {
    /**/* w  ww  . jav a  2  s .  c om*/
     * Center the dialog on the parent frame.
     *
     * @param parentFrame the parent frame
     * @param dialog the dialog
     */
    public static void centerDialogOnFrame(JFrame parentFrame, JDialog dialog) {
        Point topLeft = parentFrame.getLocationOnScreen();
        Dimension parentSize = parentFrame.getSize();
        Dimension dialogSize = dialog.getSize();
        int x;
        int y;
        if (parentSize.width > dialogSize.width) {
            x = ((parentSize.width - dialogSize.width) / 2) + topLeft.x;
        } else {
            x = topLeft.x;
        }
        if (parentSize.height > dialogSize.height) {
            y = ((parentSize.height - dialogSize.height) / 2) + topLeft.y;
        } else {
            y = topLeft.y;
        }
        dialog.setLocation(x, y);
    }
}

Related

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