1 30 31 package com.jgoodies.looks.demo; 32 33 import javax.swing.*; 34 35 import com.jgoodies.forms.builder.DefaultFormBuilder; 36 import com.jgoodies.forms.builder.PanelBuilder; 37 import com.jgoodies.forms.factories.FormFactory; 38 import com.jgoodies.forms.layout.CellConstraints; 39 import com.jgoodies.forms.layout.FormLayout; 40 import com.jgoodies.forms.layout.Sizes; 41 import com.jgoodies.looks.Options; 42 43 50 final class AlignmentTab { 51 52 private static final String TEST_STR = "EEEEE"; 53 54 60 JComponent build() { 61 FormLayout layout = new FormLayout( 62 "0:grow, center:pref, 0:grow", 63 "pref, 21dlu, pref"); 64 65 PanelBuilder builder = new PanelBuilder(layout); 66 builder.setDefaultDialogBorder(); 67 68 builder.add(createHelpLabel(), new CellConstraints(2, 1)); 69 builder.add(buildAlignmentTestPanel(), new CellConstraints(2, 3)); 70 71 return builder.getPanel(); 72 } 73 74 private JComponent buildAlignmentTestPanel() { 75 FormLayout layout = new FormLayout( 76 "p, 2px, 38dlu, 2px, 38dlu, 2px, 38dlu, 2px, max(38dlu;p)"); 77 78 DefaultFormBuilder builder = new DefaultFormBuilder(layout); 79 builder.setLineGapSize(Sizes.pixel(1)); 80 81 builder.append(createCenteredLabel("Label")); 82 builder.append(createCenteredLabel("Field")); 83 builder.append(createCenteredLabel("Edit")); 84 builder.append(createCenteredLabel("Choice")); 85 builder.append(createCenteredLabel("Button")); 86 builder.append(TEST_STR); 87 builder.append(new JTextField(TEST_STR)); 88 builder.append(createComboBox(TEST_STR, true)); 89 builder.append(createComboBox(TEST_STR, false)); 90 builder.append(createNarrowButton(TEST_STR)); 91 92 builder.appendRow(FormFactory.PARAGRAPH_GAP_ROWSPEC); 93 builder.nextLine(2); 94 95 builder.append(createCenteredLabel("Label")); 96 builder.append(createCenteredLabel("Field")); 97 builder.append(createCenteredLabel("Spinner")); 98 builder.append(createCenteredLabel("Combo")); 99 builder.append(createCenteredLabel("Button")); 100 builder.append(TEST_STR); 101 builder.append(new JTextField(TEST_STR)); 102 builder.append(createSpinner(TEST_STR)); 103 builder.append(createComboBox(TEST_STR, true)); 104 builder.append(createNarrowButton(TEST_STR)); 105 106 builder.appendRow(FormFactory.PARAGRAPH_GAP_ROWSPEC); 107 builder.nextLine(2); 108 109 builder.append(createCenteredLabel("Label")); 110 builder.append(createCenteredLabel("Field")); 111 builder.append(createCenteredLabel("Format")); 112 builder.append(createCenteredLabel("Pass")); 113 builder.append(createCenteredLabel("Button")); 114 builder.append(TEST_STR); 115 builder.append(new JTextField(TEST_STR)); 116 builder.append(new JFormattedTextField(TEST_STR)); 117 builder.append(new JPasswordField(TEST_STR)); 118 builder.append(createNarrowButton(TEST_STR)); 119 120 builder.appendRow(FormFactory.PARAGRAPH_GAP_ROWSPEC); 121 builder.nextLine(2); 122 123 builder.append(createCenteredLabel("Label")); 124 builder.append(createCenteredLabel("Field")); 125 builder.append(createCenteredLabel("Area")); 126 builder.append(createCenteredLabel("Pane")); 127 builder.append(createCenteredLabel("Button")); 128 builder.append(TEST_STR); 129 builder.append(new JTextField(TEST_STR)); 130 builder.append(createWrappedTextArea(TEST_STR)); 131 builder.append(createWrappedEditorPane(TEST_STR)); 132 builder.append(createNarrowButton(TEST_STR)); 133 134 return builder.getPanel(); 135 } 136 137 138 140 private JComponent createHelpLabel() { 141 return new JLabel("Texts shall be aligned, perceived bounds centered."); 142 } 143 144 private JLabel createCenteredLabel(String text) { 145 return new JLabel(text, JLabel.CENTER); 146 } 147 148 private JComboBox createComboBox(String selectedText, boolean editable) { 149 JComboBox box = new JComboBox(new String [] { 150 selectedText, "1", "2", "3", "4", "5", "Two", "Three", "Four", }); 151 box.setEditable(editable); 152 return box; 153 } 154 155 private JButton createNarrowButton(String text) { 156 JButton button = new JButton(text); 157 button.putClientProperty(Options.IS_NARROW_KEY, Boolean.TRUE); 158 return button; 159 } 160 161 private JComponent createWrappedTextArea(String text) { 162 JTextArea area = new JTextArea(text); 163 return new JScrollPane(area, 164 JScrollPane.VERTICAL_SCROLLBAR_NEVER, 165 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 166 } 167 168 private JComponent createWrappedEditorPane(String text) { 169 JEditorPane pane = new JEditorPane(); 170 pane.setText(text); 171 return new JScrollPane(pane, 172 JScrollPane.VERTICAL_SCROLLBAR_NEVER, 173 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 174 } 175 176 private JSpinner createSpinner(String choice) { 177 JSpinner spinner = new JSpinner(); 178 spinner.setModel(new SpinnerListModel(new String [] { 179 choice, choice + "1", choice + "2"})); 180 return spinner; 181 } 182 183 184 } | Popular Tags |