tags:

views:

122

answers:

3

While working in Java, I find it hard to position my Main application window in the very center of the screen when I start the application.

Is there any way I can do that? I tried with some of the predefined methods but none worked.

BTW it doesn't have to be vertically centered, horizontal alignment is the more important goal for me. But vertical alignment is also welcome.

Thanks

+2  A: 

I always did it in this way:

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);

where this is the JFrame involved.

Jack
Thank you so much!Exactly what I tried to do but failed :/
AmateurProgrammer
+4  A: 

Use setLocationRelativeTo(null);

JRL
+4  A: 

You can call JFrame.setLocationRelativeTo(null) to center the window.

Greg