Java AWT Toolkit get screen size and center window
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class Main extends JComponent { protected static void displayGUI(final String title, final JComponent component) { // create window with title final JFrame frame = new JFrame(title); // set window's close button to exit application frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // place component in the center using BorderLayout frame.getContentPane().add(component, BorderLayout.CENTER); // size window based on layout frame.pack();/*from ww w .j av a 2 s . co m*/ // center window Dimension scrnSize = Toolkit.getDefaultToolkit().getScreenSize(); int scrnWidth = frame.getSize().width; int scrnHeight = frame.getSize().height; int x = (scrnSize.width - scrnWidth) / 2; int y = (scrnSize.height - scrnHeight) / 2; frame.setLocation(x, y); frame.setVisible(true); } public static void main(String[] args) { final Main c = new Main(); c.setPreferredSize(new Dimension(290, 227)); SwingUtilities.invokeLater(new Runnable() { public void run() { displayGUI("Creating a GUI", c); } }); } }