Showing JFrame at the Center of the Screen

The simplest way to get your java application JFrame to show at the center of the screen if you are using J2SE 1.4+, then you have access to the Window method:

setLocationRelativeTo(Component c)

For Example:

this.setLocationRelativeTo(null);

Use can use null for the argument (in place of component arg) to center your top–level container or your JFrame

Or the other possible way to do so is to use the following code to get it in the center:

// Get the size of the screen
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    // Determine the new location of the window
    int w = window.getSize().width;
    int h = window.getSize().height;
    int x = (dim.width-w)/2;
    int y = (dim.height-h)/2;

    // Move the window
    window.setLocation(x, y);

 

From: http://www.java-forums.org/awt-swing/3491-jframe-center-screen.html

Advertisements

, ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: