KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > ac > roe > antigen > dialogs > FrameCenterer


1 /*
2  * Created on 30-May-2005
3  *
4  * @todo To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */

7 package uk.ac.roe.antigen.dialogs;
8
9 import java.awt.Dimension JavaDoc;
10 import java.awt.Point JavaDoc;
11 import java.awt.Toolkit JavaDoc;
12
13 import javax.swing.JFrame JavaDoc;
14
15 /**
16  *
17  * Centers a given frame. Surely there's a better way?
18  * @author Jon Tayler johndavidtaylor@users.sourceforge.net
19  *
20  */

21 public class FrameCenterer {
22
23     private JFrame JavaDoc theFrame;
24
25     /**
26      *
27      */

28     public FrameCenterer(JFrame JavaDoc theFrame) {
29         this.theFrame = theFrame;
30     }
31
32     public void center() {
33         Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
34         Dimension JavaDoc d = theFrame.getSize();
35         int x = (screenSize.width - d.width) / 2;
36         int y = (screenSize.height - d.height) / 2;
37         Point JavaDoc pt = new Point JavaDoc(x, y);
38         theFrame.setLocation(pt);
39     }
40 }
41
Popular Tags