Source code: com/simscomputing/swing/SwingUtilities.java
1 package com.simscomputing.swing;
2
3 import java.awt.Container;
4 import java.awt.Dimension;
5 import java.awt.Point;
6 import java.awt.Toolkit;
7
8 import javax.swing.JDesktopPane;
9 import javax.swing.JFrame;
10
11 /**
12 Common Swing tasks.
13
14 @author <a href="http://www.simscomputing.com">Sims Computing</a>
15 @version $Revision: 1.1.1.1 $ $Date: 2000/02/21 21:22:37 $
16 */
17 public class SwingUtilities {
18 private static final Point ZERO_POINT = new Point();
19
20 /**
21 Centers the specified frame in the middle of the screen.
22 */
23 public static final void centerOnScreen(final JFrame frame) {
24 final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
25 final Dimension frameSize = frame.getSize();
26 center(frame, screenSize, ZERO_POINT);
27 } // centerOnScreen()
28
29 /**
30 Centers the specified frame in the middle of the specified parent.
31 */
32 public static final void centerOnParent(final Container container,
33 final Container parent) {
34 center(container, parent.getSize(), parent.getLocation());
35 } // centerOnParent()
36
37 /**
38 Centers the specified internal frame in the middle of the specified parent.
39 */
40 public static final void centerOnParent(final Container internalFrame,
41 final JDesktopPane parent) {
42 center(internalFrame, parent.getSize(), ZERO_POINT);//parent.getLocation());
43 } // centerOnParent()
44
45
46 private static void center(final Container inner, final Dimension outerSize,
47 final Point offset) {
48 final Dimension innerSize = inner.getSize();
49
50 // reset inner's width and height if they are too big
51 if (innerSize.width > outerSize.width) {
52 innerSize.width = outerSize.width;
53 } // if
54 if (innerSize.height > outerSize.height) {
55 innerSize.height = outerSize.height;
56 } // if
57
58 // put the inner in the center of the desktop
59 inner.setLocation((outerSize.width - innerSize.width) / 2 + offset.x,
60 (outerSize.height - innerSize.height) / 2 + offset.y);
61 } // center()
62 } // class SwingUtilities