Java tutorial
//package com.java2s; import java.awt.Component; import java.awt.Container; import java.awt.Rectangle; import javax.swing.JDialog; public class Main { /** * Sets the bounds for a dialog so it is centered over its parent. * * @param dialog * @deprecated use centerOnParent */ public static void centerDialogInParent(JDialog dialog) { centerInParent(dialog); } /** * Centers a component on its parent. * * @param component */ public static void centerInParent(Component component) { Container parent = component.getParent(); if (parent != null) { Rectangle parentBounds = parent.getBounds(); Rectangle dialogBounds = new Rectangle( (int) (parentBounds.getMinX() + parentBounds.getWidth() / 2 - component.getWidth() / 2), (int) (parentBounds.getMinY() + parentBounds.getHeight() / 2 - component.getHeight() / 2), component.getWidth(), component.getHeight()); //dialog.setBounds( dialogBounds ); component.setLocation(dialogBounds.x, dialogBounds.y); } } }