Here you can find the source of centerOnFrame(JDialog dialog, JFrame frame)
public static void centerOnFrame(JDialog dialog, JFrame frame)
//package com.java2s; //License from project: Apache License import javax.swing.*; import java.awt.*; public class Main { public static void centerOnFrame(JDialog dialog, JFrame frame) { Point frameLoc = frame.getLocation(); Dimension frameSize = frame.getSize(); Dimension dialogSize = dialog.getSize(); int dialogX = frameLoc.x + (frameSize.width - dialogSize.width) / 2; int dialogY = frameLoc.y + (frameSize.height - dialogSize.height) / 2; dialog.setLocation(dialogX, dialogY); }/* w w w. j a v a 2s. co m*/ public static void centerOnFrame(JFrame dialog, JFrame frame) { Point frameLoc = frame.getLocation(); Dimension frameSize = frame.getSize(); Dimension dialogSize = dialog.getSize(); int dialogX = frameLoc.x + (frameSize.width - dialogSize.width) / 2; int dialogY = frameLoc.y + (frameSize.height - dialogSize.height) / 2; dialog.setLocation(dialogX, dialogY); } public static void centerOnFrame(Frame dialog, JFrame frame) { Point frameLoc = frame.getLocation(); Dimension frameSize = frame.getSize(); Dimension dialogSize = dialog.getSize(); int dialogX = frameLoc.x + (frameSize.width - dialogSize.width) / 2; int dialogY = frameLoc.y + (frameSize.height - dialogSize.height) / 2; dialog.setLocation(dialogX, dialogY); } }