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.