List of usage examples for java.awt Window getSize
public Dimension getSize()
From source file:util.ui.UiUtilities.java
/** * Centers a window to its parent frame and shows it. * <p>//from w ww .j a va 2 s . c o m * If the window has no parent frame it will be centered to the screen. * * @param win * 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); }