Here you can find the source of centerOnComponet(Window target, JComponent parent)
public static void centerOnComponet(Window target, JComponent parent)
//package com.java2s; /******************************************************************************* * Copyright 2011 Krzysztof Otrebski//from w ww . j av a 2 s . com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ import javax.swing.*; import java.awt.*; public class Main { public static void centerOnComponet(Window target, JComponent parent) { Dimension targetSize = target.getSize(); Point location = parent.getLocationOnScreen(); Dimension sourceSize = parent.getSize(); Point sourceCenter = new Point(location.x + sourceSize.width / 2, location.y + sourceSize.height / 2); Point frameLocation = new Point(sourceCenter.x - targetSize.width / 2, sourceCenter.y - targetSize.height / 2); target.setLocation(frameLocation); } }