Center Jframe to screen in java application

This article will describe how to center the Jframe to the screen in a java application with example source code.Whatever the screen size is the Jframe will be centered both horizontally and vertically in the screen.Here is the source code to do this.

private void CenterJFrame(){

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

Use the CenterJFrame() function after the initComponents() function of the Jframe class.



That is all.

If you liked this article please do leave a reply and share it with friends.

Thanks.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.