1    
2    package ca.janeg.swing;
3    import java.awt.Dimension;
4    import java.awt.Toolkit;
5    import java.awt.Window;
6    
7    /**
8     *  Utility methods for Swing components.
9     * 
10    * 
11    *  @author Jane Griscti, jane@janeg.ca
12    */
13   public class Utils {
14   
15       /**
16        *  Center a component on the screen.
17        * 
18        *  Source: 
19        *  <a href="http://javaalmanac.com/egs/java.awt/screen_CenterScreen.html">
20        *  The Java Almanac</a>
21        *  @param window the component to be centered.
22        */
23       public static void center( Window window ) {
24   
25           // Get the size of the screen
26           Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
27   
28           // Determine the new location of the window
29           int w = window.getSize().width;
30           int h = window.getSize().height;
31           int x = (dim.width - w) / 2;
32           int y = (dim.height - h) / 2;
33   
34           // Move the window
35           window.setLocation(x, y);
36   
37       }
38   
39   }
40