Tuesday, May 24, 2011

Centering JFrame To Any Screen Size



You have adjusted the location of your application so that is stays right at the center of your screen on load. It looks good and works perfectly fine however, when it is deployed to other computer, you have discovered that it does not stay at the center of the screen but at the bottom. You tried another computer screen and it stays at the top when loaded. That's probably horrible.

You have to address certain situations where it is out of your control. This variable changes as the end users uses different sizes of screen. This is the objective of this discussion actually. To create a Java Frame that stays at the center of any screen at any given size.

Java has a lot of API's that can be abused to create a better application.  By using the AWT Toolkit, you're done. With it, getting the screen size is a lot easier nowadays. Armed with simple mathematical computations, then you're ready to go.

Given samples below will show you how to compute the x and y values that will be used to position the window application.
Size of the screen:
height = 2000
width = 2100

Size of your application:
height = 200
width = 300

get the x and y positions:
y = ((height of the screen /2) - (height of the window/2))
= (2000/2) - (200/2)
x = ((width of the screen/2) - (width of the application/2))
= (2100/2) - (300/2))

Actually we don't really know the screen size so translating that to java code, we have this:

/*
 * @author: theredfont
 */
package com.javaswing;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class JavaSwing implements Runnable{
 
 private JFrame frame;
 
 public JavaSwing(){
  this.init();
 }
 public void init(){  
  frame = new JFrame("Screen Centered Frame");
  frame.setSize(300, 300);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
 
 @Override
 public void run() {
  frame.setVisible(true);     
 }
 public static void main(String []argv){
  Runnable runnable = new JavaSwing();  
  SwingUtilities.invokeLater(runnable);
 }
}


We created a JFrame using the code above. The above is a working code. When the code is executed, the frame stays at the right most top corner of the screen. To make it stay at the center, follow the code below.
/*
 * @author: theredfont
 */
package com.javaswing;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import java.awt.Toolkit;

public class JavaSwing implements Runnable{
 
 private JFrame frame;
 
 Toolkit toolkit;
 public JavaSwing(){
  this.init();
 }
 public void init(){
  toolkit = Toolkit.getDefaultToolkit();   
  frame = new JFrame("Screen Centered Frame");
  frame.setSize(300, 300);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setLocation();
 }
 
 public void setLocation(){
  int screenX = toolkit.getScreenSize().width;
  int screenY = toolkit.getScreenSize().height;
  int x = ((screenX/2) - (frame.getWidth())/2);
  int y = ((screenY/2) - (frame.getHeight())/2);
  frame.setLocation(x, y);
 }
 @Override
 public void run() {  
  frame.setVisible(true);    
 }
 public static void main(String []argv){
  Runnable runnable = new JavaSwing();  
  SwingUtilities.invokeLater(runnable);
 }
}



You can see the code changes on the above.

Learn More:

No comments:

Post a Comment

There was an error in this gadget