* The window to center and show.
*/
public static void centerAndShow(Window win) {
Dimension wD = win.getSize();
Dimension frameD;
Point framePos;
Frame frame = JOptionPane.getFrameForComponent(win);
// Should this window be centered to its parent frame?
boolean centerToParentFrame = (frame != null) && (frame != win)
&& frame.isShowing();
// Center to parent frame or to screen
if (centerToParentFrame) {
frameD = frame.getSize();
framePos = frame.getLocation();
} else {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
// dual head, use first screen
if (ge.getScreenDevices().length > 1) {
try {
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration config = gd.getConfigurations()[0];
frameD = config.getBounds().getSize();
framePos = config.getBounds().getLocation();
} catch (RuntimeException e) {
frameD = Toolkit.getDefaultToolkit().getScreenSize();
framePos = new Point(0, 0);
}
}
// single screen only
else {
frameD = Toolkit.getDefaultToolkit().getScreenSize();
framePos = new Point(0, 0);
}
}
Point wPos = new Point(framePos.x + (frameD.width - wD.width) / 2,
framePos.y + (frameD.height - wD.height) / 2);
wPos.x = Math.max(0, wPos.x); // Make x > 0
wPos.y = Math.max(0, wPos.y); // Make y > 0
win.setLocation(wPos);
win.setVisible(true);