1 30 31 package com.jgoodies.looks.demo; 32 33 import java.awt.Component ; 34 35 import javax.swing.*; 36 import javax.swing.text.JTextComponent ; 37 38 import com.jgoodies.forms.builder.DefaultFormBuilder; 39 import com.jgoodies.forms.builder.PanelBuilder; 40 import com.jgoodies.forms.factories.FormFactory; 41 import com.jgoodies.forms.layout.CellConstraints; 42 import com.jgoodies.forms.layout.ColumnSpec; 43 import com.jgoodies.forms.layout.FormLayout; 44 import com.jgoodies.forms.layout.Sizes; 45 46 53 final class StatesTab { 54 55 58 JComponent build() { 59 FormLayout layout = new FormLayout( 60 "right:max(50dlu;pref), 4dlu, pref"); 61 DefaultFormBuilder builder = new DefaultFormBuilder(layout); 62 builder.setDefaultDialogBorder(); 63 64 builder.append("Standard:", buildButtonRow(true, true)); 65 builder.append("No Content:", buildButtonRow(true, false)); 66 builder.append("No Border:", buildButtonRow(false, true)); 67 builder.append("Radio Button:", buildRadioButtonRow()); 68 builder.append("Check Box:", buildCheckBoxRow()); 69 builder.append("Combo Box:", buildComboBoxRow()); 70 builder.append("Text Field:", buildTextRow(JTextField.class, false)); 71 builder.append("Formatted Field:", buildTextRow(JFormattedTextField.class, false)); 72 builder.append("Password:", buildTextRow(JPasswordField.class, false)); 73 builder.append("Text Area:", buildTextRow(JTextArea.class, true)); 74 builder.append("Editor Pane:", buildTextRow(JEditorPane.class, true)); 75 builder.append("Spinner:", buildSpinnerRow()); 76 77 return builder.getPanel(); 78 } 79 80 81 83 private JComponent buildButtonRow( 84 boolean borderPainted, 85 boolean contentAreaFilled) { 86 JButton button = new JButton("Standard"); 87 button.setDefaultCapable(true); 88 89 return buildButtonRow( 90 new AbstractButton[] { 91 button, 92 new JToggleButton("Selected"), 93 new JButton("Disabled"), 94 new JToggleButton("Selected")}, 95 borderPainted, 96 contentAreaFilled); 97 } 98 99 private JComponent buildCheckBoxRow() { 100 return buildButtonRow( 101 new AbstractButton[] { 102 new JCheckBox("Deselected"), 103 new JCheckBox("Selected"), 104 new JCheckBox("Disabled"), 105 new JCheckBox("Selected")}, 106 false, 107 false); 108 } 109 110 private JComponent buildRadioButtonRow() { 111 return buildButtonRow( 112 new AbstractButton[] { 113 new JRadioButton("Deselected"), 114 new JRadioButton("Selected"), 115 new JRadioButton("Disabled"), 116 new JRadioButton("Selected")}, 117 false, 118 false); 119 } 120 121 private JComponent buildButtonRow( 122 AbstractButton[] buttons, 123 boolean borderPainted, 124 boolean contentAreaFilled) { 125 buttons[1].setSelected(true); 126 buttons[2].setEnabled(false); 127 buttons[3].setEnabled(false); 128 buttons[3].setSelected(true); 129 for (int i = 0; i < buttons.length; i++) { 130 buttons[i].setBorderPainted(borderPainted); 131 buttons[i].setContentAreaFilled(contentAreaFilled); 132 } 133 134 return buildGrid(buttons[0], 135 buttons[1], 136 buttons[2], 137 buttons[3], 138 FormFactory.BUTTON_COLSPEC); 139 } 140 141 143 149 private JComponent buildTextRow(Class textComponentClass, boolean wrap) { 150 JTextComponent [] components = new JTextComponent [4]; 151 for (int i = 0; i < 4; i++) { 152 try { 153 components[i] = 154 (JTextComponent ) textComponentClass.newInstance(); 155 } catch (InstantiationException e) { 156 } catch (IllegalAccessException e) { 158 } 160 } 161 components[0].setText("Editable"); 162 163 components[1].setText("Uneditable"); 164 components[1].setEditable(false); 165 166 components[2].setText("Disabled"); 167 components[2].setEnabled(false); 168 169 components[3].setText("Uneditable"); 170 components[3].setEditable(false); 171 components[3].setEnabled(false); 172 173 return wrap 174 ? buildGrid( 175 wrapWithScrollPane(components[0]), 176 wrapWithScrollPane(components[1]), 177 wrapWithScrollPane(components[2]), 178 wrapWithScrollPane(components[3])) 179 : buildGrid( 180 components[0], 181 components[1], 182 components[2], 183 components[3]); 184 } 185 186 private Component wrapWithScrollPane(Component c) { 187 return new JScrollPane( 188 c, 189 ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER, 190 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 191 } 192 193 195 private JComponent buildComboBoxRow() { 196 return buildGrid( 197 createComboBox("Editable", true, true), 198 createComboBox("Uneditable", true, false), 199 createComboBox("Disabled", false, true), 200 createComboBox("Uneditable", false, false)); 201 } 202 203 private JComboBox createComboBox( 204 String text, 205 boolean enabled, 206 boolean editable) { 207 JComboBox box = 208 new JComboBox(new String [] { text, "Two", "Three", "Four", "A Quite Long Label" }); 209 box.setEnabled(enabled); 210 box.setEditable(editable); 211 return box; 215 } 216 217 private JComponent buildSpinnerRow() { 218 return buildGrid(createSpinner(true), 219 new JPanel(null), 220 createSpinner(false), 221 new JPanel(null)); 222 } 223 224 private JComponent createSpinner(boolean enabled) { 225 JSpinner spinner = new JSpinner(); 226 spinner.setEnabled(enabled); 227 return spinner; 228 } 229 230 231 233 266 267 269 private JComponent buildGrid( 270 Component c1, 271 Component c2, 272 Component c3, 273 Component c4) { 274 return buildGrid(c1, c2, c3, c4, 275 new ColumnSpec(ColumnSpec.DEFAULT, 276 Sizes.dluX(20), 277 ColumnSpec.DEFAULT_GROW)); 278 } 279 280 private JComponent buildGrid( 281 Component c1, 282 Component c2, 283 Component c3, 284 Component c4, 285 ColumnSpec colSpec) { 286 FormLayout layout = new FormLayout("", "pref"); 287 for (int i = 0; i < 4; i++) { 288 layout.appendColumn(colSpec); 289 layout.appendColumn(FormFactory.RELATED_GAP_COLSPEC); 290 } 291 PanelBuilder builder = new PanelBuilder(layout); 292 CellConstraints cc = new CellConstraints(); 293 builder.add(c1, cc.xy(1, 1)); 294 builder.add(c2, cc.xy(3, 1)); 295 builder.add(c3, cc.xy(5, 1)); 296 builder.add(c4, cc.xy(7, 1)); 297 return builder.getPanel(); 298 } 299 300 301 } | Popular Tags |