How to Show JFrame on Center of Screen in NetBeans Java Swings

To center a JFrame on the screen in a Java Swing application,  Create a project with your choice and create a JFrame in it. Once you successfully created the JFrame, on the top you should see 3 tabs Source, Design and History. Design your Jframe as you like. To edit the auto-generated code you must click on the source buttion.

By default when you run the JFrame it shows to the left top side of the screen, to make it appear in the center of the screen just set the property as shown in code below.

setLocationRelativeTo(null);

Here is more information about the code.

By setting the property to setLocationRelativeTo(null) – This line of code shows the frame to appear in the center of the screen. When you pass null as the argument, it causes the window to be placed in the center of the screen.

Make sure you put the piece of code in your constructor after the initComponents();  as shown in the code.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top