com.genedavis.view.ViewUtil

This is an old class I wrote that contains a method for centering Java windows and frames on the current screen. Just pass in a JFrame or a Window which are both Components, and they get centered on the screen.

package com.genedavis.view;

import java.awt.*;

/**
 * Reuse/modify at will. Written around 2001.
 */
public class ViewUtil {

	public static void centerWindow(Component frame) {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize  = frame.getSize();

		if (frameSize.width  > screenSize.width )
			frameSize.width  = screenSize.width;

		if (frameSize.height > screenSize.height)
			frameSize.height = screenSize.height;

		frame.setLocation (
		  (screenSize.width  - frameSize.width ) >> 1,
		  (screenSize.height - frameSize.height) >> 1
		);
	}
}