Java tutorial
//package com.java2s; import java.awt.Component; import java.awt.Dialog; import java.awt.Dimension; import java.awt.Point; import java.awt.Toolkit; import java.awt.Window; public class Main { public static Window showCentered(Component parent, Window window) { if (null == window) throw new IllegalArgumentException("window null"); if (window instanceof Dialog) { Dialog dialog = (Dialog) window; boolean isResizable = dialog.isResizable(); dialog.setResizable(true); dialog.pack(); dialog.setResizable(isResizable); } else { window.pack(); } Dimension windowSize = window.getPreferredSize(); int newX; int newY; if (null == parent) { Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); newX = (screen.width - windowSize.width) / 2; newY = (screen.height - windowSize.height) / 2; } else { Dimension parentSize = parent.getSize(); Point loc = parent.getLocation(); newX = (parentSize.width - windowSize.width) / 2 + loc.x; newY = (parentSize.height - windowSize.height) / 2 + loc.y; if (0 > newX) newX = 0; if (0 > newY) newY = 0; } window.setLocation(newX, newY); window.setVisible(true); return window; } public static Window showCentered(Window window) { return showCentered(null, window); } }