Search This Blog

Showing posts with label center of screen. Show all posts
Showing posts with label center of screen. Show all posts

How to Get Center Point of Screen in Java

If you want position your JFrame in center of screen, there is method for that. Read this post:

The LocalGraphicsEnvironment in Java will give you the center point of the display or screen. There is a static method getLocalGraphicsEnvironment() in GraphicsEnvironment class. To get the center point of the screen, you may use the following java code:


Point centre=GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();

You should include these import statements in the file.

import java.awt.Point; import java.awt.GraphicsEnvironment;

How to Set JFrame in Center of Screen

By default, a java application appears at top left corner of the screen. You might have seen that most of the applications appear in the middle of the screen. I will tell you how to center your java application on screen.

To set JFrame in center of Just call this.setLocationRelativeTo(null); in constructor of the JFrame. If you are using Netbeans IDE, it should be after calling initComponents() method. But if you call setLocation() method of JFrame after this, your JFrame will get relocated.

In most cases this works and this is enough. if you want to get the center point of the screen, just refer to the following post:

How to Get Center Point of Screen in Java