1 30 31 package com.jgoodies.looks.demo; 32 33 import java.awt.Component ; 34 import java.awt.GridLayout ; 35 36 import javax.swing.Box ; 37 import javax.swing.JButton ; 38 import javax.swing.JComponent ; 39 import javax.swing.JPanel ; 40 41 import com.jgoodies.forms.builder.PanelBuilder; 42 import com.jgoodies.forms.layout.CellConstraints; 43 import com.jgoodies.forms.layout.FormLayout; 44 import com.jgoodies.looks.Options; 45 46 54 final class NarrowTab { 55 56 59 JComponent build() { 60 FormLayout fl = new FormLayout( 61 "7dlu, right:max(50dlu;pref), 4dlu, left:pref, 0:grow", 62 "pref, 2dlu, pref, 4dlu, pref, 4dlu, pref, 11dlu, " 63 + "pref, 2dlu, pref, 4dlu, pref, 4dlu, pref, 11dlu, " 64 + "pref, 2dlu, pref, 4dlu, pref, 4dlu, pref, 0:grow"); 65 PanelBuilder builder = new PanelBuilder(fl); 66 builder.setDefaultDialogBorder(); 67 68 CellConstraints cc = new CellConstraints(); 69 70 builder.addSeparator("Unmodified Button Widths (BoxLayout)", cc.xyw(1, 1, 5)); 72 73 builder.addLabel("No Narrow Hint:", cc.xy(2, 3)); 74 builder.add(buildButtonBoxNoNarrow(), cc.xy(4, 3)); 75 76 builder.addLabel("One Narrow Hint:", cc.xy(2, 5)); 77 builder.add(buildButtonBoxOneNarrow(), cc.xy(4, 5)); 78 79 builder.addLabel("All Narrow Hints:", cc.xy(2, 7)); 80 builder.add(buildButtonBoxAllNarrow(), cc.xy(4, 7)); 81 82 builder.addSeparator("Adjusted Button Widths (FormLayout)", cc.xyw(1, 9, 5)); 84 85 builder.addLabel("No Narrow Hint:", cc.xy(2, 11)); 86 builder.add(buildButtonFormNoNarrow(), cc.xy(4, 11)); 87 88 builder.addLabel("One Narrow Hint:", cc.xy(2, 13)); 89 builder.add(buildButtonFormOneNarrow(), cc.xy(4, 13)); 90 91 builder.addLabel("All Narrow Hints:", cc.xy(2, 15)); 92 builder.add(buildButtonFormAllNarrow(), cc.xy(4, 15)); 93 94 builder.addSeparator("Equalized Button Widths (GridLayout)", cc.xyw(1, 17, 5)); 96 97 builder.addLabel("No Narrow Hint:", cc.xy(2, 19)); 98 builder.add(buildButtonGridNoNarrow(), cc.xy(4, 19)); 99 100 builder.addLabel("One Narrow Hint:", cc.xy(2, 21)); 101 builder.add(buildButtonGridOneNarrow(), cc.xy(4, 21)); 102 103 builder.addLabel("All Narrow Hints:", cc.xy(2, 23)); 104 builder.add(buildButtonGridAllNarrow(), cc.xy(4, 23)); 105 106 return builder.getPanel(); 107 } 108 109 111 private Component buildButtonBoxNoNarrow() { 112 return buildButtonBox(createButtons()); 113 } 114 115 private Component buildButtonBoxOneNarrow() { 116 return buildButtonBox(createButtonsWithOneNarrowHint()); 117 } 118 119 private Component buildButtonBoxAllNarrow() { 120 return buildButtonBox(createNarrowHintedButtons()); 121 } 122 123 private Component buildButtonBox(JButton [] buttons) { 124 Box box = Box.createHorizontalBox(); 125 for (int i = 0; i < buttons.length; i++) { 126 box.add(buttons[i]); 127 box.add(Box.createHorizontalStrut(6)); 128 } 129 return box; 130 } 131 132 134 private Component buildButtonFormNoNarrow() { 135 return buildButtonForm(createButtons()); 136 } 137 138 private Component buildButtonFormOneNarrow() { 139 return buildButtonForm(createButtonsWithOneNarrowHint()); 140 } 141 142 private Component buildButtonFormAllNarrow() { 143 return buildButtonForm(createNarrowHintedButtons()); 144 } 145 146 private Component buildButtonForm(JButton [] buttons) { 147 FormLayout fl = new FormLayout( 148 "pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref", 149 "pref"); 150 fl.setColumnGroups(new int[][]{{1, 3, 5, 7}}); 151 JPanel panel = new JPanel (fl); 152 for (int i = 0; i < buttons.length; i++) { 153 panel.add(buttons[i], new CellConstraints(i * 2 + 1, 1)); 154 } 155 return panel; 156 } 157 158 160 private Component buildButtonGridNoNarrow() { 161 return buildButtonGrid(createButtons()); 162 } 163 164 private Component buildButtonGridOneNarrow() { 165 return buildButtonGrid(createButtonsWithOneNarrowHint()); 166 } 167 168 private Component buildButtonGridAllNarrow() { 169 return buildButtonGrid(createNarrowHintedButtons()); 170 } 171 172 private Component buildButtonGrid(JButton [] buttons) { 173 JPanel grid = new JPanel (new GridLayout (1, 4, 6, 0)); 174 for (int i = 0; i < buttons.length; i++) { 175 grid.add(buttons[i]); 176 } 177 return grid; 178 } 179 180 182 185 private JButton createNarrowButton(String text) { 186 JButton button = new JButton (text); 187 button.putClientProperty(Options.IS_NARROW_KEY, Boolean.TRUE); 188 return button; 189 } 190 191 194 private JButton [] createButtons() { 195 return new JButton [] { 196 new JButton ("Add..."), 197 new JButton ("Remove"), 198 new JButton ("Up"), 199 new JButton ("Down"), 200 new JButton ("A Long Label")}; 201 } 202 203 206 private JButton [] createButtonsWithOneNarrowHint() { 207 return new JButton [] { 208 new JButton ("Add..."), 209 new JButton ("Remove"), 210 new JButton ("Up"), 211 new JButton ("Down"), 212 createNarrowButton("A Long Label")}; 213 } 214 215 218 private JButton [] createNarrowHintedButtons() { 219 return new JButton [] { 220 createNarrowButton("Add..."), 221 createNarrowButton("Remove"), 222 createNarrowButton("Up"), 223 createNarrowButton("Down"), 224 createNarrowButton("A Long Label")}; 225 } 226 227 228 } | Popular Tags |