1 package jimm.datavision.gui; 2 import java.util.ArrayList ; 3 import java.util.Iterator ; 4 import java.awt.*; 5 import javax.swing.*; 6 import javax.swing.border.Border ; 7 import java.awt.GridBagLayout ; 8 import java.awt.GridBagConstraints ; 9 10 19 public class EditFieldLayout { 20 21 22 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 rows; 34 protected Border border; 35 protected JPanel panel; 36 37 public EditFieldLayout() { 38 rows = new ArrayList (); 39 } 40 41 53 public Component add(String 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 69 public JTextField addTextField(String label) { 70 return (JTextField)add(label, new JTextField()); 71 } 72 73 80 public JTextField addTextField(String label, int columns) { 81 return (JTextField)add(label, new JTextField(columns)); 82 } 83 84 91 public JTextField addTextField(String label, String text) { 92 return (JTextField)add(label, new JTextField(text == null ? "" : text)); 93 } 94 95 103 public JTextField addTextField(String label, String text, int columns) { 104 return (JTextField)add(label, new JTextField(text == null ? "" : text, 105 columns)); 106 } 107 108 114 public JTextArea addTextArea(String label) { 115 JTextArea area = new JTextArea(); 116 area.setBorder(BorderFactory.createLoweredBevelBorder()); 117 add(label, area); 118 return area; 119 } 120 121 129 public JTextArea addTextArea(String 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 143 public JTextArea addTextArea(String label, String text) { 144 JTextArea area = new JTextArea(text == null ? "" : text); 145 area.setBorder(BorderFactory.createLoweredBevelBorder()); 146 add(label, area); 147 return area; 148 } 149 150 159 public JTextArea addTextArea(String label, String 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 172 public JCheckBox addCheckBox(String label) { 173 return addCheckBox(label, 0); 174 } 175 176 183 public JCheckBox addCheckBox(String label, int key) { 184 JCheckBox checkBox = new JCheckBox(label); 185 checkBox.setMnemonic(key); 186 return (JCheckBox)add(null, checkBox); 187 } 188 189 196 public JComboBox addComboBox(String label, Object [] items) { 197 return addComboBox(label, items, false); 198 } 199 200 209 public JComboBox addComboBox(String label, Object [] items, boolean editable) { 210 JComboBox comboBox = new JComboBox(items); 211 comboBox.setEditable(editable); 212 return (JComboBox)add(label, comboBox); 213 } 214 215 222 public JLabel addLabel(String label, String text) { 223 return (JLabel)add(label, new JLabel(text == null ? "" : text)); 224 } 225 226 232 public JPasswordField addPasswordField(String label) { 233 return (JPasswordField)add(label, new JPasswordField()); 234 } 235 236 243 public JPasswordField addPasswordField(String label, int columns) { 244 return (JPasswordField)add(label, new JPasswordField(columns)); 245 } 246 247 254 public JPasswordField addPasswordField(String label, String password) { 255 return (JPasswordField)add(label, new JPasswordField(password == null 256 ? "" : password)); 257 } 258 259 267 public JPasswordField addPasswordField(String label, String password, 268 int columns) 269 { 270 return (JPasswordField)add(label, new JPasswordField(password == null 271 ? "" : password, 272 columns)); 273 } 274 275 281 public JButton addButton(String label) { 282 return (JButton)add(null, new JButton(label)); 283 } 284 285 288 public void skipRow() { 289 rows.add(null); 290 } 291 292 297 public void setBorder(int allSides) { 298 setBorder(allSides, allSides, allSides, allSides); 299 } 300 301 309 public void setBorder(int top, int left, int bottom, int right) { 310 border = BorderFactory.createEmptyBorder(top, left, bottom, right); 311 } 312 313 319 public JPanel getPanel() { 320 if (panel == null) 321 buildPanel(); 322 return panel; 323 } 324 325 328 protected void buildPanel() { 329 GridBagLayout bag = new GridBagLayout (); 330 GridBagConstraints c = new GridBagConstraints (); 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 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 |