If you want to add a nice touch to your GUI application, center your window on the screen. Not only does this looks much better than having it start up in the default (0, 0) location, but it takes very little coding effort to make this happen. Fortunately for us, there are two methods for doing this, depending on which version of Java you are using. I will show you how to do it both ways.
Using Java 1.4 or later
If you are using Java 1.4 or later, you can use just one method from the Window class to center your frame on the screen, regardless of the user’s screen resolution.
The Code
Assuming you are extending JFrame, you can add this to your initialization method:
this.setLocationRelativeTo( null );
If you are not extending JFrame but rather instantiating a new JFrame in your code, you can do something like this instead:
// Assuming `frame` is the name of your JFrame frame.setLocationRelativeTo( null );
Setting the location relative to null automatically centers the frame on the screen. If you’re not using Java 1.4 or later, use the below method instead.
Prior to Java 1.4
Prior to Java 1.4, you will want to get the user’s screen resolution and then calculate where to place your JFrame on the screen using the below code:
The Code
Assuming you are extending JFrame in your GUI class file, add the following method to the source:
private void centerWindow() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation( dim.width / 2 - this.getWidth() / 2, dim.height / 2 - this.getHeight() / 2 );
}
Then, upon initialization, you will need to call this method. For example:
public void initialize() {
...
this.centerWindow();
...
}
If you are NOT extending JFrame in your class file (i.e. you are instantiating a new JFrame somewhere instead), then you need to change the this to the frame name, or a method that returns the frame reference. For example, if the method getJFrame() returns our JFrame, then we can do the following instead:
private void centerWindow() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
getJFrame().setLocation( dim.width / 2 - getJFrame().getWidth() / 2, dim.height / 2 - getJFrame().getHeight() / 2 );
}
Again, upon initialization, you will need to call this method:
public void initialize() {
...
getJFrame().centerWindow();
...
}
The latter part of the code really depends on how you designed the class.
A Full Example
If you’re still confused by the above, try the full example below:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CenterFrameTest extends JFrame {
private JPanel jContentPane = null;
private static final long serialVersionUID = 1L;
/**
* Default constructor
*/
public CenterFrameTest() {
super();
initialize();
}
/**
* Main method
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
CenterFrameTest thisClass = new CenterFrameTest();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This method centers the frame on
* the screen.
*/
private void centerWindow() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation( dim.width / 2 - this.getWidth() / 2, dim.height / 2 - this.getHeight() / 2 );
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
}
return jContentPane;
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("Center Window Example");
this.centerWindow(); // Call to center the window
}
}
Enjoy!
