How do I go about positioning a JDialog at the center of the screen?
Asked
Active
Viewed 9.5k times
5 Answers
167
In Java 1.4+ you can do:
final JDialog d = new JDialog();
d.setSize(200,200);
d.setLocationRelativeTo(null);
d.setVisible(true);
Or perhaps (pre 1.4):
final JDialog d = new JDialog();
d.setSize(200, 200);
final Toolkit toolkit = Toolkit.getDefaultToolkit();
final Dimension screenSize = toolkit.getScreenSize();
final int x = (screenSize.width - d.getWidth()) / 2;
final int y = (screenSize.height - d.getHeight()) / 2;
d.setLocation(x, y);
d.setVisible(true);

johnstok
- 96,212
- 12
- 54
- 76
-
7what exactly is happening when you setlocation relative to null? – Mark Norgren Aug 19 '09 at 13:30
-
6Wanted to add that you do need to use setSize() or else the setLocationRelativeTo() will not work. I had to use both setSize() AND setPreferredSize() in order to get everything to look right. – Richard Jun 16 '11 at 13:35
-
2In answer to the question by @marked: If the component is not currently showing, or c is null, the window is placed at the center of the screen. (from the java.awt.Window javadocs) – Jeremy Brooks Feb 21 '12 at 21:14
-
2Note that both of these methods will center the dialog *on the primary monitor* (at least on Windows). Better to pass an argument to `setLocationRelativeTo` so the dialog appears on the appropriate monitor for multi-monitor users. – Brad Mace Aug 28 '14 at 13:04
-
5I had to call `pack()` before `setLocationRelativeTo(null)` to properly center the dialog. – Matthias Braun Nov 30 '15 at 10:35
-
1This solution still works with Java 8! To actually position it in the center, the order has to be: 1. `pack()`, 2. `setLocationRelativeTo(this)`, 3. `setVisible(true)`. Any other order and the dialog won't be centered but in the top left corner! – Neph Sep 12 '19 at 09:55
-
This helps for putting a `JDialog` that contains an instance of `JOptionPane` in the center. – Tech Expert Wizard Nov 19 '20 at 15:25
-
When you call dialog.setLocationRelativeTo(null) statement before dialog.setSize(...) , dialog doesn't appear at the center of the screen. – Pekmezli Dürüm Feb 19 '23 at 14:56
11
Use this line after the pack()
method:
setLocation((Toolkit.getDefaultToolkit().getScreenSize().width)/2 - getWidth()/2, (Toolkit.getDefaultToolkit().getScreenSize().height)/2 - getHeight()/2);

Corey Adler
- 15,897
- 18
- 66
- 80

Kunax
- 111
- 1
- 2
-
This is too complicated. It requires manual positioning of the dialog to the center according to the dialog's size and the screen size. – Tech Expert Wizard Nov 19 '20 at 15:27
8
Two helpers for centering within the screen or within the parent.
// Center on screen ( absolute true/false (exact center or 25% upper left) )
public void centerOnScreen(final Component c, final boolean absolute) {
final int width = c.getWidth();
final int height = c.getHeight();
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screenSize.width / 2) - (width / 2);
int y = (screenSize.height / 2) - (height / 2);
if (!absolute) {
x /= 2;
y /= 2;
}
c.setLocation(x, y);
}
// Center on parent ( absolute true/false (exact center or 25% upper left) )
public void centerOnParent(final Window child, final boolean absolute) {
child.pack();
boolean useChildsOwner = child.getOwner() != null ? ((child.getOwner() instanceof JFrame) || (child.getOwner() instanceof JDialog)) : false;
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
final Dimension parentSize = useChildsOwner ? child.getOwner().getSize() : screenSize ;
final Point parentLocationOnScreen = useChildsOwner ? child.getOwner().getLocationOnScreen() : new Point(0,0) ;
final Dimension childSize = child.getSize();
childSize.width = Math.min(childSize.width, screenSize.width);
childSize.height = Math.min(childSize.height, screenSize.height);
child.setSize(childSize);
int x;
int y;
if ((child.getOwner() != null) && child.getOwner().isShowing()) {
x = (parentSize.width - childSize.width) / 2;
y = (parentSize.height - childSize.height) / 2;
x += parentLocationOnScreen.x;
y += parentLocationOnScreen.y;
} else {
x = (screenSize.width - childSize.width) / 2;
y = (screenSize.height - childSize.height) / 2;
}
if (!absolute) {
x /= 2;
y /= 2;
}
child.setLocation(x, y);
}

Java42
- 7,628
- 1
- 32
- 50
5
here's my solution to retrieve screen dimension with multiple monitors.
import java.awt.*;
import javax.swing.JFrame;
/**
* Méthodes statiques pour récupérer les informations d'un écran.
*
* @author Jean-Claude Stritt
* @version 1.0 / 24.2.2009
*/
public class ScreenInfo {
/**
* Permet de récupérer le numéro de l'écran par rapport à la fenêtre affichée.
* @return le numéro 1, 2, ... (ID) de l'écran
*/
public static int getScreenID( JFrame jf ) {
int scrID = 1;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gd = ge.getScreenDevices();
for (int i = 0; i < gd.length; i++) {
GraphicsConfiguration gc = gd[i].getDefaultConfiguration();
Rectangle r = gc.getBounds();
if (r.contains(jf.getLocation())) {
scrID = i+1;
}
}
return scrID;
}
/**
* Permet de récupérer la dimension (largeur, hauteur) en px d'un écran spécifié.
* @param scrID --> le n° d'écran
* @return la dimension (largeur, hauteur) en pixels de l'écran spécifié
*/
public static Dimension getScreenDimension( int scrID ) {
Dimension d = new Dimension(0, 0);
if (scrID > 0) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
DisplayMode mode = ge.getScreenDevices()[scrID - 1].getDisplayMode();
d.setSize(mode.getWidth(), mode.getHeight());
}
return d;
}
/**
* Permet de récupérer la largeur en pixels d'un écran spécifié.
* @param scrID --> le n° d'écran
* @return la largeur en px de l'écran spécifié
*/
public static int getScreenWidth( int scrID ) {
Dimension d = getScreenDimension(scrID);
return d.width;
}
/**
* Permet de récupérer la hauteur en pixels d'un écran spécifié.
* @param scrID --> le n° d'écran
* @return la hauteur en px de l'écran spécifié
*/
public static int getScreenHeight( int scrID ) {
Dimension d = getScreenDimension(scrID);
return d.height;
}
}
3
AFAIK you can pass a GraphicEnvironment to each JDialog/JFrame/JWindow constructor. This object describes the monitor to use.

ZeissS
- 11,867
- 4
- 35
- 50