Java tutorial
//package com.java2s; import java.awt.Component; import java.awt.Dimension; import java.awt.Point; import java.awt.Window; public class Main { /** * Centers a component according to the window location. * @param wnd The parent window * @param cmp A component, usually a dialog */ public static void centerInWindow(Window wnd, Component cmp) { Dimension size = wnd.getSize(); Point loc = wnd.getLocationOnScreen(); Dimension cmpSize = cmp.getSize(); loc.x += (size.width - cmpSize.width) / 2; loc.y += (size.height - cmpSize.height) / 2; cmp.setBounds(loc.x, loc.y, cmpSize.width, cmpSize.height); } }