12

Sample code:

    JFrame jFrame = new JFrame("Test");
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jFrame.setLocationRelativeTo(null);
    jFrame.setSize(600, 600);
    jFrame.pack();
    // jFrame.setLocationRelativeTo(null); // same results
    jFrame.setVisible(true);

screenshot

Is this the OpenJDK's fault? I recall hearing it wasn't as good as Sun's, but since it became the standard for Ubuntu or whatever I decided to go along with it. The program is probably gonna run on windows, so I suppose I'm gonna have to check there... Any easy way to fix this in a platform independent way without breaking it where it already works?

Community
  • 1
  • 1
captain poop
  • 215
  • 1
  • 2
  • 5
  • In you screenshot, the top-left corner is perfectly centered. You just need to offset it by the height and width of the frame. – jjnguy Aug 13 '10 at 19:36
  • 7
    As pointed out by Evan, this code is calling setLocationRelativeTo() too early in the code. It must be done **after** pack()/setSize(). Also note that in both your example and Evan's, the call to setSize() is redundant if immediately followed by pack(). – Andrew Thompson Mar 18 '11 at 12:25

7 Answers7

22
JFrame jFrame = new JFrame("Test");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//jFrame.setLocationRelativeTo(null);
jFrame.setSize(600, 600);
jFrame.pack();
jFrame.setVisible(true);
jFrame.setLocationRelativeTo(null); //To center the code

This will correct the problem and center the Jframe

Evan
  • 296
  • 3
  • 4
  • 8
    Basically, call pack() before setLocationRelativeTo(). This is because pack() computes the size of the window, which is required to perform the centering calculation correctly. – Greg Mattes Jan 22 '13 at 16:38
5

One way is to manually position the window. Put the following code right after your call to pack().

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Point middle = new Point(screenSize.width / 2, screenSize.height / 2);
Point newLocation = new Point(middle.x - (jFrame.getWidth() / 2), 
                              middle.y - (jFrame.getHeight() / 2));
jFrame.setLocation(newLocation);

Disclaimer, this was only tested on windows.

Also, you should always use setPreferredSize() instead of setSize().

jjnguy
  • 136,852
  • 53
  • 295
  • 323
  • @captain, you get the same results as in your question? – jjnguy Aug 13 '10 at 19:37
  • @captain, try running it without dividing getWidth and getHeight by 2. So: `new Point(middle.x - (jFrame.getWidth()), middle.y - (jFrame.getHeight()));` – jjnguy Aug 13 '10 at 19:41
  • @Justin same thing happened. It's weird because I can set the location normally, but neither of those methods worked. Well, I realize that the problem is the jFrame getWidth() and getHeight(). Width is 10, Height is 30, even though I've set the jframe size to 600 x 600. – captain poop Aug 13 '10 at 19:45
  • @captain, what do you mean by "set the location normally'? – jjnguy Aug 13 '10 at 19:46
  • @cap, also, use `setPreferredSize()` instead of `setSize()` – jjnguy Aug 13 '10 at 19:47
  • @Justin: aha! the problem was setSize(). I had to use setPreferredSize(). Thanks! – captain poop Aug 13 '10 at 19:49
4

Just a precision : If you set the location before the size of the frame, you will center the top left corner of the window because the size is (0,0). You have to set the size before the location.

JFrame jFrame = new JFrame("Test");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setSize(600, 600);
jFrame.pack();
jFrame.setLocationRelativeTo(null);
jFrame.setVisible(true);

It works well with me with OpenJDK-6 and Ubuntu 13.04. Try it on other platforms.

YoannFleuryDev
  • 914
  • 1
  • 12
  • 22
3
jFrame.validate();

This actually works better since pack can change the frame size, while validate leaves the frame size alone.

chown
  • 51,908
  • 16
  • 134
  • 170
2

I know this is an old question, but setLocationRelativeTo() will work but it must be called after pack(). Frame's getWidth() and getHeight() return different (correct) values after packing and that's why OP is unable to center.

mrazjava
  • 21
  • 1
  • 1
1

Just set the size before setting the location.

Wrong:

jFrame.setLocationRelativeTo(null);
jFrame.setSize(600, 600);

Correct:

jFrame.setSize(600, 600);
jFrame.setLocationRelativeTo(null);

Note: Call setVisible() at last to prevent "jumping" of the window.

trinity420
  • 670
  • 7
  • 19
0

You should not declare the jFrame size before giving the relative location. If you do that what happens is that will draw your iFrame away from the given location.

This is wrong----

JFrame jFrame = new JFrame("Test");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setLocationRelativeTo(null);
jFrame.setSize(600, 600);
jFrame.pack();
jFrame.setVisible(true);

This is right----

JFrame jFrame = new JFrame("Test");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//size comes first
jFrame.setSize(600, 600);

//and then the position
jFrame.setLocationRelativeTo(null);

jFrame.pack();
jFrame.setVisible(true);
LahiruBandara
  • 931
  • 1
  • 9
  • 14
  • In your comments you said size comes first then you pack the `JFrame` after `setLocationRelativeTo`... – Dan Aug 17 '17 at 06:27