1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.util;
21
22 import java.awt.Canvas;
23 import java.awt.Component;
24 import java.awt.Container;
25 import java.awt.GridBagConstraints;
26 import java.awt.GridBagLayout;
27 import java.awt.Insets;
28
29 import javax.swing.JLabel;
30
31 /***
32 * This helper class provides convenience methods to add components to a
33 * <code>GridBagLayout</code>.
34 */
35 public class GridBagHelper {
36
37
38
39 public static final int X_SPACING = 5;
40
41 /***
42 * The default insets for components.
43 */
44 public static final Insets COMPONENT_INSETS = new Insets(5, 5, 5, 5);
45
46 /***
47 * The default insets for labels.
48 */
49 public static final Insets LABEL_INSETS = new Insets(7, 5, 0, 5);
50
51 /***
52 * The default insets for panels.
53 */
54 public static final Insets PANEL_INSETS = new Insets(5, 5, 0, 5);
55
56 /***
57 * The default insets for strings.
58 */
59 public static final Insets STRING_INSETS = new Insets(5, 0, 0, 5);
60
61
62
63 public static void add(Container p, Component c, Insets insets, boolean fill,
64 int anchor)
65 {
66 GridBagConstraints gbc = new GridBagConstraints();
67 gbc.gridwidth = GridBagConstraints.REMAINDER;
68 gbc.anchor = anchor;
69 if (fill) {
70 gbc.fill = GridBagConstraints.HORIZONTAL;
71 }
72 gbc.insets = insets;
73 gbc.weightx = 1;
74 ((GridBagLayout)p.getLayout()).setConstraints(c, gbc);
75 p.add(c);
76 }
77
78 public static void add(Container p, Component c, Insets insets, boolean fill)
79 {
80 add(p, c, insets, fill, GridBagConstraints.NORTHWEST);
81 }
82
83 public static void add(Container p, Component c, boolean fill)
84 {
85 add(p, c, COMPONENT_INSETS, fill);
86 }
87
88 public static void add(Container p, Component c)
89 {
90 add(p, c, COMPONENT_INSETS, true);
91 }
92
93 public static JLabel add(Container p, String s)
94 {
95 JLabel l = new JLabel(s);
96 add(p, l, STRING_INSETS, true);
97 return l;
98 }
99
100 /***
101 * Adds <code>c</code> without a break.
102 */
103 public static void addComponent(Container p, Component c, int anchor)
104 {
105 GridBagConstraints gbc = new GridBagConstraints();
106 gbc.gridwidth = 1;
107 gbc.anchor = anchor;
108 gbc.insets = COMPONENT_INSETS;
109 ((GridBagLayout)p.getLayout()).setConstraints(c, gbc);
110 p.add(c);
111 }
112
113 /***
114 * Adds <code>c</code> without a break.
115 */
116 public static void addComponent(Container p, Component c)
117 {
118 addComponent(p, c, GridBagConstraints.NORTHWEST);
119 }
120
121 /***
122 * Adds a label to <code>p</code> that displays <code>s</code>. The
123 * label is placed a bit more south than the rest of the controls.
124 *
125 * @return the added label
126 */
127 public static JLabel addLabel(Container p, String s, boolean fill)
128 {
129 JLabel l = new JLabel(s);
130 GridBagConstraints gbc = new GridBagConstraints();
131 gbc.gridwidth = (fill) ? GridBagConstraints.REMAINDER : 1;
132 gbc.anchor = GridBagConstraints.NORTHWEST;
133 gbc.insets = LABEL_INSETS;
134 ((GridBagLayout)p.getLayout()).setConstraints(l, gbc);
135 p.add(l);
136
137 return l;
138 }
139
140 /***
141 * Adds a horizontal spacer to <code>p</code> that sucks up the remaining
142 * space. All components that have been added to p are pushed eastwards.
143 */
144 public static void addHorizontalSpacer(Container p)
145 {
146 Canvas c = new Canvas();
147 GridBagConstraints gbc = new GridBagConstraints();
148 gbc.gridwidth = GridBagConstraints.REMAINDER;
149 gbc.fill = GridBagConstraints.HORIZONTAL;
150 gbc.weightx = 1;
151 ((GridBagLayout)p.getLayout()).setConstraints(c, gbc);
152 p.add(c);
153 }
154
155 /***
156 * Fill is set to false.
157 *
158 * @see #addLabel(Container, String, boolean)
159 */
160 public static JLabel addLabel(Container p, String s)
161 {
162 return addLabel(p, s, false);
163 }
164
165 public static void addPanel(Container p, Component c)
166 {
167 GridBagConstraints gbc = new GridBagConstraints();
168 gbc.gridwidth = GridBagConstraints.REMAINDER;
169 gbc.fill = GridBagConstraints.BOTH;
170 gbc.insets = PANEL_INSETS;
171 gbc.weightx = 1;
172 gbc.weighty = 1;
173 ((GridBagLayout)p.getLayout()).setConstraints(c, gbc);
174 p.add(c);
175 }
176
177 /***
178 * Adds a vertical spacer to <code>p</code> that sucks up the remaining
179 * space. All components that have been added to p are pushed northwards.
180 */
181 public static void addVerticalSpacer(Container p)
182 {
183 Canvas c = new Canvas();
184 GridBagConstraints gbc = new GridBagConstraints();
185 gbc.gridwidth = GridBagConstraints.REMAINDER;
186 gbc.fill = GridBagConstraints.VERTICAL;
187 gbc.weighty = 1;
188 ((GridBagLayout)p.getLayout()).setConstraints(c, gbc);
189 p.add(c);
190 }
191
192 }