KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > gui > EditFieldLayout


1 package jimm.datavision.gui;
2 import java.util.ArrayList JavaDoc;
3 import java.util.Iterator JavaDoc;
4 import java.awt.*;
5 import javax.swing.*;
6 import javax.swing.border.Border JavaDoc;
7 import java.awt.GridBagLayout JavaDoc;
8 import java.awt.GridBagConstraints JavaDoc;
9
10 /**
11  * Lays out a bunch of label/edit widget pairs. Optionally creates
12  * the edit widget for you. This is not a layout manager <i>per se</i>.
13  * Calling any of the <code>add</code>* methods creates label/edit
14  * widget pairs. Calling <code>getPanel</code> returns a panel containing
15  * the labels and edit widgets, arranged for your pleasure.
16  *
17  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
18  */

19 public class EditFieldLayout {
20
21 /* ================================================================ */
22 /** Represents a label/component pair. */
23 static class Row {
24 protected JLabel label;
25 protected Component component;
26 Row(JLabel l, Component c) {
27     label = l;
28     component = c;
29 }
30 }
31 /* ================================================================ */
32
33 protected ArrayList JavaDoc rows;
34 protected Border JavaDoc border;
35 protected JPanel panel;
36
37 public EditFieldLayout() {
38     rows = new ArrayList JavaDoc();
39 }
40
41 /**
42  * Adds the label/component pair to the layout and returns the component.
43  * If <var>label</var> does not end with a colon, one will be added.
44  * <var>label</var> may be <code>null</code>, in which case no label is
45  * displayed.
46  * <p>
47  * All the other <code>add*</code> methods call this one.
48  *
49  * @param label a possibly <code>null</code> label string
50  * @param c a GUI component
51  * @return the component
52  */

53 public Component add(String JavaDoc label, Component c) {
54     if (label == null || label.length() == 0)
55     label = "";
56     else if (!label.endsWith(":"))
57     label += ":";
58
59     rows.add(new Row(new JLabel(label), c));
60     return c;
61 }
62
63 /**
64  * Creates a text field and adds it and the label.
65  *
66  * @param label a possibly <code>null</code> label string
67  * @return the new text field
68  */

69 public JTextField addTextField(String JavaDoc label) {
70     return (JTextField)add(label, new JTextField());
71 }
72
73 /**
74  * Creates a text field and adds it and the label.
75  *
76  * @param label a possibly <code>null</code> label string
77  * @param columns the text field's size
78  * @return the new text field
79  */

80 public JTextField addTextField(String JavaDoc label, int columns) {
81     return (JTextField)add(label, new JTextField(columns));
82 }
83
84 /**
85  * Creates a text field and adds it and the label.
86  *
87  * @param label a possibly <code>null</code> label string
88  * @param text the text field's initial text
89  * @return the new text field
90  */

91 public JTextField addTextField(String JavaDoc label, String JavaDoc text) {
92     return (JTextField)add(label, new JTextField(text == null ? "" : text));
93 }
94
95 /**
96  * Creates a text field and adds it and the label.
97  *
98  * @param label a possibly <code>null</code> label string
99  * @param text the text field's initial text
100  * @param columns the text field's size
101  * @return the new text field
102  */

103 public JTextField addTextField(String JavaDoc label, String JavaDoc text, int columns) {
104     return (JTextField)add(label, new JTextField(text == null ? "" : text,
105                          columns));
106 }
107
108 /**
109  * Creates a text area and adds it and the label.
110  *
111  * @param label a possibly <code>null</code> label string
112  * @return the new text area
113  */

114 public JTextArea addTextArea(String JavaDoc label) {
115     JTextArea area = new JTextArea();
116     area.setBorder(BorderFactory.createLoweredBevelBorder());
117     add(label, area);
118     return area;
119 }
120
121 /**
122  * Creates a text area and adds it and the label.
123  *
124  * @param label a possibly <code>null</code> label string
125  * @param rows the text field's height
126  * @param cols the text field's width
127  * @return the new text area
128  */

129 public JTextArea addTextArea(String JavaDoc label, int rows, int cols) {
130     JTextArea area = new JTextArea(rows, cols);
131     area.setBorder(BorderFactory.createLoweredBevelBorder());
132     add(label, area);
133     return area;
134 }
135
136 /**
137  * Creates a text area and adds it and the label.
138  *
139  * @param label a possibly <code>null</code> label string
140  * @param text the text field's initial text
141  * @return the new text area
142  */

143 public JTextArea addTextArea(String JavaDoc label, String JavaDoc text) {
144     JTextArea area = new JTextArea(text == null ? "" : text);
145     area.setBorder(BorderFactory.createLoweredBevelBorder());
146     add(label, area);
147     return area;
148 }
149
150 /**
151  * Creates a text area and adds it and the label.
152  *
153  * @param label a possibly <code>null</code> label string
154  * @param text the text field's initial text
155  * @param rows the text field's height
156  * @param cols the text field's width
157  * @return the new text area
158  */

159 public JTextArea addTextArea(String JavaDoc label, String JavaDoc text, int rows, int cols) {
160     JTextArea area = new JTextArea(text == null ? "" : text, rows, cols);
161     JScrollPane scroller = new JScrollPane(area);
162     add(label, scroller);
163     return area;
164 }
165
166 /**
167  * Creates a check box and adds it and the label.
168  *
169  * @param label a possibly <code>null</code> label string
170  * @return the new check box
171  */

172 public JCheckBox addCheckBox(String JavaDoc label) {
173     return addCheckBox(label, 0);
174 }
175
176 /**
177  * Creates a check box and adds it and the label.
178  *
179  * @param label a possibly <code>null</code> label string
180  * @param key the mnemonic key (a <code>KeyEvent</code> constant)
181  * @return the new check box
182  */

183 public JCheckBox addCheckBox(String JavaDoc label, int key) {
184     JCheckBox checkBox = new JCheckBox(label);
185     checkBox.setMnemonic(key);
186     return (JCheckBox)add(null, checkBox);
187 }
188
189 /**
190  * Creates a combo box and adds it and the label.
191  *
192  * @param label a possibly <code>null</code> label string
193  * @param items an array of objects
194  * @return the new combo box
195  */

196 public JComboBox addComboBox(String JavaDoc label, Object JavaDoc[] items) {
197     return addComboBox(label, items, false);
198 }
199
200 /**
201  * Creates a combo box and adds it and the label.
202  *
203  * @param label a possibly <code>null</code> label string
204  * @param items an array of objects
205  * @param editable if <code>true</code>, the combo box will allow custom
206  * value entry by the user
207  * @return the new combo box
208  */

209 public JComboBox addComboBox(String JavaDoc label, Object JavaDoc[] items, boolean editable) {
210     JComboBox comboBox = new JComboBox(items);
211     comboBox.setEditable(editable);
212     return (JComboBox)add(label, comboBox);
213 }
214
215 /**
216  * Creates two labels and adds them.
217  *
218  * @param label a possibly <code>null</code> label string
219  * @param text text for the right-hand label
220  * @return the new right-hand label
221  */

222 public JLabel addLabel(String JavaDoc label, String JavaDoc text) {
223     return (JLabel)add(label, new JLabel(text == null ? "" : text));
224 }
225
226 /**
227  * Creates a password field and adds it and the label.
228  *
229  * @param label a possibly <code>null</code> label string
230  * @return the new password field
231  */

232 public JPasswordField addPasswordField(String JavaDoc label) {
233     return (JPasswordField)add(label, new JPasswordField());
234 }
235
236 /**
237  * Creates a password field and adds it and the label.
238  *
239  * @param label a possibly <code>null</code> label string
240  * @param columns the password field's size
241  * @return the new password field
242  */

243 public JPasswordField addPasswordField(String JavaDoc label, int columns) {
244     return (JPasswordField)add(label, new JPasswordField(columns));
245 }
246
247 /**
248  * Creates a password field and adds it and the label.
249  *
250  * @param label a possibly <code>null</code> label string
251  * @param password the initial password text
252  * @return the new password field
253  */

254 public JPasswordField addPasswordField(String JavaDoc label, String JavaDoc password) {
255     return (JPasswordField)add(label, new JPasswordField(password == null
256                              ? "" : password));
257 }
258
259 /**
260  * Creates a password field and adds it and the label.
261  *
262  * @param label a possibly <code>null</code> label string
263  * @param password the initial password text
264  * @param columns the password field's size
265  * @return the new password field
266  */

267 public JPasswordField addPasswordField(String JavaDoc label, String JavaDoc password,
268                        int columns)
269 {
270     return (JPasswordField)add(label, new JPasswordField(password == null
271                              ? "" : password,
272                              columns));
273 }
274
275 /**
276  * Creates a button and adds it to the right-hand side, under the fields.
277  *
278  * @param label a button label
279  * @return the new button
280  */

281 public JButton addButton(String JavaDoc label) {
282     return (JButton)add(null, new JButton(label));
283 }
284
285 /**
286  * Creates an empty row.
287  */

288 public void skipRow() {
289     rows.add(null);
290 }
291
292 /**
293  * Creates an empty border the same size on all sides.
294  *
295  * @param allSides the width of the border
296  */

297 public void setBorder(int allSides) {
298     setBorder(allSides, allSides, allSides, allSides);
299 }
300
301 /**
302  * Creates an empty border on all sides.
303  *
304  * @param top top border size
305  * @param left left border size
306  * @param bottom bottom border size
307  * @param right right border size
308  */

309 public void setBorder(int top, int left, int bottom, int right) {
310     border = BorderFactory.createEmptyBorder(top, left, bottom, right);
311 }
312
313 /**
314  * Returns the panel containing all the labels and edit widgets. Lazily
315  * instantiates the panel.
316  *
317  * @return the panel containing all the labels and edit widgets
318  */

319 public JPanel getPanel() {
320     if (panel == null)
321     buildPanel();
322     return panel;
323 }
324
325 /**
326  * Builds the panel.
327  */

328 protected void buildPanel() {
329     GridBagLayout JavaDoc bag = new GridBagLayout JavaDoc();
330     GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
331     c.insets = new Insets(6, 6, 6, 6);
332     panel = new JPanel();
333     panel.setLayout(bag);
334     if (border != null)
335     panel.setBorder(border);
336
337     c.gridy = 0;
338     for (Iterator JavaDoc iter = rows.iterator(); iter.hasNext(); ++c.gridy) {
339     Row row = (Row)iter.next();
340     if (row == null)
341         continue;
342
343     if (row.label != null) {
344         c.gridx = 0;
345         c.anchor = GridBagConstraints.NORTHEAST;
346         bag.setConstraints(row.label, c);
347         panel.add(row.label);
348     }
349
350     if (row.component != null) {
351         c.gridx = 1;
352         c.anchor = GridBagConstraints.NORTHWEST;
353         bag.setConstraints(row.component, c);
354         panel.add(row.component);
355     }
356     }
357 }
358
359 }
360
Popular Tags