Thread: center a form
View Single Post
  #3 (permalink)  
Old 08-06-2007, 10:47 PM
gabriel gabriel is offline
Member
 
Join Date: Jul 2007
Posts: 41
gabriel is on a distinguished road
This works fine
Code:
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(); }
good luck
Reply With Quote