16

I'm developing a Java Swing application. How can I make it so that when the program itself and any other windows open they come up in the center of the screen?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Travis
  • 161
  • 1
  • 1
  • 3

5 Answers5

46
frame.setLocationRelativeTo( null );
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Does that work also on older versions of Java ? I'm surprised because a while ago I figured out the only way to do that was "by hand", as explained here: http://www.java2s.com/Code/Java/Swing-JFC/Howtocenteraframeordialog.htm – Jules Olléon Jun 21 '10 at 03:55
  • 1
    That is valid as of 1.4, but you have to make sure that you pack or otherwise size the screen correctly first. – Yishai Jun 21 '10 at 04:09
  • 2
    Not really obvious when you don't know it, but the behavior of [setLocationRelativeTo()](http://tinyurl.com/34syhcj), with a null, is indeed to center the Window, as described in the javadoc. – Gnoupi Jun 21 '10 at 08:16
  • It doesn't centre the window - it centres on the top left of the window. – Dan Jan 05 '15 at 14:19
  • 4
    @DanMatthews-Grout, this is because you need to invoke frame.pack() or frame.setSize() BEFORE you invoke frame.setLocationRelativeTo(...). Otherwise the size of the frame is (0, 0) so location is based on that size. – camickr Jan 05 '15 at 15:38
4

frame.setLocationRelativeTo(null); make the frame open in cetner

Regards, Rehan Farooq

3

It is easy if you are using Netbeans. In property window of JFrame go to "Code" tab.There is an option as "Generate center".Check that option. JFrame will display in center.

Saji
  • 43
  • 4
1

You'll have to do it by hand, using setLocation(x,y).

Something like:

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((dim.width-frameWidth)/2, (dim.height-frameHeight)/2);

should do it (not tested).

Jules Olléon
  • 6,733
  • 6
  • 37
  • 47
1

Doing it by hand in multi-screen environment gives something like this (static, 'cause you probably would want it in a utility class):

  public static Rectangle getScreenBounds(Component top){
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gd = ge.getScreenDevices();

    if (top != null){
        Rectangle bounds = top.getBounds();
        int centerX = (int) bounds.getCenterX();
        int centerY = (int) bounds.getCenterY();

        for (GraphicsDevice device : gd){
            GraphicsConfiguration gc = device.getDefaultConfiguration();
            Rectangle r = gc.getBounds();
            if (r.contains(centerX, centerY)){
                return r;
            }
        }
    }
    return gd[0].getDefaultConfiguration().getBounds();
}

public void centerWindowOnScreen(Window windowToCenter){
    Rectangle bounds = getScreenBounds(windowToCenter);
    Point newPt = new Point(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
    Dimension componentSize = windowToCenter.getSize();
    newPt.x -= componentSize.width / 2;
    newPt.y -= componentSize.height / 2;
    windowToCenter.setLocation(newPt);

}

As for default button, it's JDialog.getRootPane().setDefaultButton(btn), but button has to be already added to the dialog, and visible.

Taisin
  • 491
  • 2
  • 6
  • I work with two displays here. One is even vertical shifted. camickr/Rehans answers works for me. It centers the window on the first screen. Isn't that what your method try's to accomplish? Or am I missing something here? Thanks! – Riscie Feb 01 '14 at 19:04
  • @Riscie not really. My method is trying to find on which screen your component is shown, so you can center it on this screen, not only on the first. – Taisin Feb 05 '14 at 09:14
  • thanks for clarifying! Well thats pretty need! A shame this is not offered by default! – Riscie Feb 06 '14 at 11:54