Here you can find the source of centerDialogOnFrame(JFrame parentFrame, JDialog dialog)
Parameter | Description |
---|---|
parentFrame | the parent frame |
dialog | the dialog |
public static void centerDialogOnFrame(JFrame parentFrame, JDialog dialog)
//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); } }