KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > CenterLayout


1 /*
2  * @(#)CenterLayout.java 1.12 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.plaf.basic;
9
10
11 import java.awt.*;
12 import java.io.*;
13
14 /**
15   * Center-positioning layout manager.
16   * @version 1.12 12/19/03
17   * @author Tom Santos
18   * @author Steve Wilson
19   */

20 class CenterLayout implements LayoutManager, Serializable {
21     public void addLayoutComponent(String JavaDoc name, Component comp) { }
22     public void removeLayoutComponent(Component comp) { }
23
24     public Dimension preferredLayoutSize( Container container ) {
25     Component c = container.getComponent( 0 );
26     if ( c != null ) {
27             Dimension size = c.getPreferredSize();
28             Insets insets = container.getInsets();
29
30             return new Dimension(size.width + insets.left + insets.right,
31                                  size.height + insets.top + insets.bottom);
32     }
33     else {
34         return new Dimension( 0, 0 );
35     }
36     }
37
38     public Dimension minimumLayoutSize(Container cont) {
39         return preferredLayoutSize(cont);
40     }
41
42     public void layoutContainer(Container container) {
43         if (container.getComponentCount() > 0) {
44             Component c = container.getComponent(0);
45             Dimension pref = c.getPreferredSize();
46             int containerWidth = container.getWidth();
47             int containerHeight = container.getHeight();
48             Insets containerInsets = container.getInsets();
49
50             containerWidth -= containerInsets.left +
51                               containerInsets.right;
52             containerHeight -= containerInsets.top +
53                                containerInsets.bottom;
54
55             int left = (containerWidth - pref.width) / 2 +
56                             containerInsets.left;
57             int right = (containerHeight - pref.height) / 2 +
58                             containerInsets.top;
59
60             c.setBounds(left, right, pref.width, pref.height);
61         }
62     }
63 }
64
Popular Tags