1 package snow.utils.gui; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 import javax.swing.event.*; 7 import javax.swing.border.*; 8 import java.util.*; 9 10 13 public class GridLayout3 extends GridBagLayout 14 { 15 int cols; 16 JPanel target; 17 final public GridBagConstraints constr = new GridBagConstraints(); 18 19 final Hashtable<Integer , Integer > columnAligmnents = new Hashtable<Integer , Integer >(); 20 21 24 public GridLayout3(int cols, JPanel target) 25 { 26 super(); 27 28 this.cols = cols; 29 this.target = target; 30 31 constr.gridwidth = 1; 32 constr.anchor = GridBagConstraints.WEST; 33 constr.insets = new Insets(1,5,1,1); 34 35 target.setLayout(this); 36 37 } 39 42 public void setColumnAlignment(int col, int alignment) 43 { 44 columnAligmnents.put(new Integer (col), new Integer (alignment)); 45 } 46 47 private int actualCol = 0; 49 private double[] weights = null; 50 51 53 public void setColumnWeights(double[] w) 54 { 55 this.weights = w; 56 } 57 58 public JLabel add(String a) 59 { 60 JLabel jl = new JLabel(a); 61 jl.setOpaque(false); 62 add(jl, false); 63 return jl; 64 } 65 66 68 public JComponent add(JComponent comp) 69 { 70 add(comp, false); 71 return comp; 72 } 73 74 75 76 public JComponent addTitleSeparator(String title) 77 { 78 return addTitleSeparator(title, TitledBorder.DEFAULT_JUSTIFICATION); 80 } 81 82 83 public JComponent addTitleSeparator(String title, int TitledBorderjustification) 84 { 85 return addTitleSeparator(title, 87 UIManager.getColor("TitledBorder.titleColor"), 88 TitledBorderjustification, TitledBorder.DEFAULT_POSITION, 89 UIManager.getFont("Label.font").getSize(), 0); 90 } 91 92 97 public JComponent addTitleSeparator(String title, Color lineColor, 98 int TitledBorderjustification, int TitledBorderposition, int spaceAbove, int spaceBelow) 99 { 100 constr.gridwidth = constr.REMAINDER; 101 constr.fill = constr.HORIZONTAL; 102 JPanel pan = new JPanel(new BorderLayout(0,0)); 103 pan.setBorder( 104 BorderFactory.createCompoundBorder( 105 BorderFactory.createEmptyBorder(spaceAbove, 0, spaceBelow, 0), 106 BorderFactory.createTitledBorder( 107 BorderFactory.createMatteBorder(1,0,0,0, lineColor), 108 title, 109 TitledBorderjustification, 110 TitledBorderposition))); 111 112 this.setConstraints(pan, constr); 113 target.add(pan); 114 actualCol = 0; 115 return pan; 116 } 117 118 public JComponent addSeparator() 119 { 120 constr.gridwidth = constr.REMAINDER; 121 constr.fill = constr.HORIZONTAL; 122 JPanel pan = new JPanel(new BorderLayout(0,0)); 123 pan.setBorder( 124 BorderFactory.createEmptyBorder(10, 0, 0, 0) 125 ); 126 this.setConstraints(pan, constr); 127 target.add(pan); 128 actualCol = 0; 129 return pan; 130 } 131 132 public JComponent add(JComponent comp, boolean fillHorizontally) 133 { 134 constr.fill = (fillHorizontally? constr.HORIZONTAL : constr.NONE); 135 136 if(actualCol%cols==cols-1) 137 { 138 constr.gridwidth = constr.REMAINDER; 140 } 141 else if(enforceLineBreakAfter) 142 { 143 constr.gridwidth = constr.REMAINDER; 144 } 145 else 146 { 147 constr.gridwidth = 1; 149 } 150 151 152 if(this.columnAligmnents.containsKey(actualCol)) 153 { 154 constr.anchor = this.columnAligmnents.get(actualCol); 155 } 156 else 157 { 158 constr.anchor = constr.WEST; 160 } 161 162 163 if(weights!=null && actualCol<weights.length) 164 { 165 constr.weightx = weights[actualCol]; 166 } 167 else 168 { 169 if(actualCol==0) 171 { 172 constr.weightx = 0; 173 } 174 else 175 { 176 constr.weightx = 1000; 177 } 178 } 179 180 181 this.setConstraints(comp, constr); 182 target.add(comp); 183 184 actualCol++; 185 if(actualCol == cols) 186 { 187 actualCol = 0; 188 } 189 190 if(enforceLineBreakAfter) 192 { 193 enforceLineBreakAfter = false; 195 actualCol = 0; 196 } 197 return comp; 198 199 } 200 201 boolean enforceLineBreakAfter = false; 202 203 205 public void insertLineBreakAfterNextComponent() 206 { 207 enforceLineBreakAfter = true; 208 211 } 212 213 214 217 public static void main( String [] arguments ) 218 { 219 test1(); 220 } 221 222 223 public static void test1() 224 { 225 JPanel panel = new JPanel(); 226 227 GridLayout3 gl = new GridLayout3(3, panel); 228 gl.setColumnAlignment(0, GridBagConstraints.EAST); 229 gl.setColumnWeights(new double[]{0,1000,0}); 230 231 JFrame jf = new JFrame("test"); 232 233 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 234 235 jf.setContentPane(panel); 236 237 gl.add( "a"); 238 gl.add( new JTextField(8), true); 239 gl.add( new JLabel("mm"), false); 240 241 gl.add( "b"); 242 gl.add( new JTextField(6), true); 243 gl.add( new JLabel("cm"), false); 244 245 gl.addTitleSeparator("Hello"); 246 247 gl.add( "Hello"); 248 gl.add( new JTextField(), true); 249 gl.add( new JLabel("km"), false); 250 251 gl.addTitleSeparator("Hello", Color.yellow, TitledBorder.CENTER, TitledBorder.ABOVE_TOP, 100, 50); 252 253 jf.pack(); 254 jf.setLocationRelativeTo(null); 255 jf.setVisible(true); 256 257 } 259 260 public static void test2() 261 { 262 JFrame frame = new JFrame("GridLayout3 line break test"); 263 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 264 265 JPanel content = new JPanel(); 266 frame.getContentPane().add(content, BorderLayout.CENTER); 267 GridLayout3 gl3 = new GridLayout3(5,content); 268 269 for(int i=0; i<20; i++) 270 { 271 JButton jb = new JButton(getRandomString()); 272 273 if(i==2) 274 { 275 gl3.insertLineBreakAfterNextComponent(); 276 jb.setBackground(Color.red); 277 } 278 279 gl3.add(jb); 280 281 } 282 283 frame.pack(); 285 frame.setLocationRelativeTo(null); 286 frame.setVisible(true); 287 } 288 289 public static String getRandomString() 290 { 291 int len = (int) (Math.random()*30) + 1; 292 StringBuffer sb = new StringBuffer (); 293 for(int i=0; i<len; i++) 294 { 295 sb.append(""+(char)('a'+i)); 296 } 297 return sb.toString(); 298 } 299 } | Popular Tags |