[robsite.org]

« At JavaOne (like everyone else it seems) | Main | Getting extension versions in JDeveloper 10.1.3 EA »

July 14, 2005

setLocationRelativeTo

If you've used a Swing GUI Builder, or have a custom utility to center your frame/windows, you probably have run into code like:

  public void centerWindow( Window w )
  {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension windowSize = w.getSize();
    if (windowSize.height > screenSize.height)
    {
      windowSize.height = screenSize.height;
    }
    if (windowSize.width > screenSize.width)
    {
      windowSize.width = screenSize.width;
    }
    w.setLocation( ( screenSize.width - windowSize.width ) / 2, ( screenSize.height - windowSize.height ) / 2 );
  }

Many IDE's put something like this in the main method of the default swing Application.

If you are using JDK 1.4 or later, you can use the method setLocationRelativeTo to replace all of that code.

Here's an example:

  myWindow.setLocationRelativeTo(null); 

Posted by rcleveng at July 14, 2005 01:56 PM

Comments

Post a comment




Remember Me?