public static void centerFrame(JFrame frame) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Point center = ge.getCenterPoint();
Rectangle bounds = ge.getMaximumWindowBounds();
int w = Math.max(bounds.width/2, Math.min(frame.getWidth(), bounds.width));
int h = Math.max(bounds.height/2, Math.min(frame.getHeight(), bounds.height));
int x = center.x - w/2, y = center.y - h/2;
frame.setBounds(x, y, w, h);
if (w == bounds.width && h == bounds.height)
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.validate();
}