I have my own main Frame and two JDialogs that pop up when someone chooses menu items.What i'd like is to have a Help dialog with some rules,suggestions etc. setLayout(null) and the Toolkit/Dimension do NOT help of course because they center the upper left corner of the frame/dialog in the screen . How could have the center of both the frame and dialog centered in a screen? Thanks in advance !
-
1For frame positioning, you cannot go by `setLocationByPlatform(true)`. See [this answer](http://stackoverflow.com/a/7143398/418556) for demo. (It includes a combination of the two methods.) – Andrew Thompson Aug 10 '13 at 18:40
5 Answers
Do these in order:
1) Call pack()
or setSize(...)
on the frame/dialog to be shown (it should already have its components added at this point).
2) Then, call setLocationRelativeTo(null)
on that container. Alternatively, you could call setLocationRelativeTo(parent)
if the parent is offset and you want the dialog to be centered within the parent at its current location.
3) Then, call setVisible(true)

- 5,357
- 2
- 22
- 45
I think that will help:
frame.setLocationRelativeTo(null);
Usually we put a null
as an argument, but you can put an argument of type Component
, I prefer JOptionPane
as it's always shows in center of the screen.
frame.setLocationRelativeTo(new JOptionPane());
//OR
frame.setLocationRelativeTo(JOptionPane.getRootFrame());
- And one hint for your, don't use
null
layout(absolute positioning), always useLayoutManagers
.

- 5,047
- 20
- 38
-
-
Did you tried? With this code, the center of frame will be exactly on the center of the screen, not the upper left corner. – Azad Aug 10 '13 at 18:36
-
-
@elig: That's good, and make sure to call this method after `frame.setSize()` ;). – Azad Aug 10 '13 at 18:38
-
but why should you create a random component just to avoid null, this is not a smart way, you are wasting resources – Dennis Kriechel Aug 10 '13 at 18:40
-
I must be doing sth wrong.I write this.setLocationRelativeTo(JOptionPane.getRootFrame()); , save and get the same thing :( – eli g Aug 10 '13 at 18:41
-
@Recall: I didn't say you must put a component, I said you can put a component too, yes, it's wasting a resource. – Azad Aug 10 '13 at 18:42
-
And i do not even use setSize() cause my other "small" components in the frame determine the total size – eli g Aug 10 '13 at 18:42
-
@Azad yes he should properly set the location of his dialogs relative to his mainframe – Dennis Kriechel Aug 10 '13 at 18:43
-
setLocationRelativeTo(null)
will only center the upper left corner if you Dialog
has no size yet, e.g. if you call the .pack()
method after the setLocationRelativeTo
.
Small example:
JFrame testFrame = new JFrame();
JButton testButton = new JButton();
testButton.setPreferredSize(new Dimension(500, 500));
testFrame.add(testButton);
testFrame.pack();
testFrame.setLocationRelativeTo(null);
testFrame.setVisible(true);
This will show an Frame which center is centered on the screen.

- 3,719
- 14
- 40
- 62
-
that's what i'm trying to say.How to center the center of a frame in the screen not the upper left corner. – eli g Aug 10 '13 at 18:33
-
as i said you need to set the size of you dialog, the method setLocationRelativeTo(null) 'centers the center' as you call it but if you have no size the center is the left top corner, the method is working right, mabye you can show some more code – Dennis Kriechel Aug 10 '13 at 18:35
-
@elig look at the edit this is a small example which works well – Dennis Kriechel Aug 10 '13 at 18:38
-
Thank you ! I've been playing with the settings for hours for this simple graphics thing. – eli g Aug 10 '13 at 18:51
-
@elig does this mean your problem is solved? Or you're still trying? – Dennis Kriechel Aug 10 '13 at 18:53
-
Use this helper method to center any JFrame or JDialog:
public static Window centerFrame(Window win) {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
win.setLocation((int) ((dim.getWidth() - win.getWidth()) / 2), (int) ((dim.getHeight() - win.getHeight()) / 2));
return win;
}

- 11,233
- 9
- 54
- 64
-
but that's what i said in the description. Dimension/toolkit and setLayout(null) both do the same thing but they center the north left corner of the frame/dialog. – eli g Aug 10 '13 at 18:26
-
No, this code takes window size into account, that is why it works. The only tricky moment here is that you should invoke it after pack(). Before pack() any Window has no computed size. – Jk1 Aug 10 '13 at 18:50
-
Thanks, but having setLayoutTo(null) after the pack() solved the problem with smaller code. :) – eli g Aug 10 '13 at 18:53
Try to add this to your code:
frameName.setLocationRelativeTo(null);
This will center the frame in your screen. One thing, make sure that you declare the size of your frame BEFORE this line of code. This line should be right before your setVisible
declaration