KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > utils > gui > GridLayout3


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 /** A grid layout using gridbag layout.
11     It has better resizes features than the GridLayout2
12 */

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 JavaDoc, Integer JavaDoc> columnAligmnents = new Hashtable<Integer JavaDoc, Integer JavaDoc>();
20
21   /** USAGE: add the component to this layout with
22      add(comp, true/false), not with the panel add !
23   */

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   } // Constructor
38

39   /** @param alignment is one of GridBagConstraints.WEST, ...
40       if not specified, default is WEST
41   */

42   public void setColumnAlignment(int col, int alignment)
43   {
44      columnAligmnents.put(new Integer JavaDoc(col), new Integer JavaDoc(alignment));
45   }
46
47   // used as a counter
48
private int actualCol = 0;
49   private double[] weights = null;
50
51   /** Example: give {0,10,0} to cause only the second column to be resized
52   */

53   public void setColumnWeights(double[] w)
54   {
55      this.weights = w;
56   }
57
58   public JLabel add(String JavaDoc a)
59   {
60      JLabel jl = new JLabel(a);
61      jl.setOpaque(false);
62      add(jl, false);
63      return jl;
64   }
65
66   /** don't fill
67   */

68   public JComponent add(JComponent comp)
69   {
70      add(comp, false);
71      return comp;
72   }
73
74
75
76   public JComponent addTitleSeparator(String JavaDoc title)
77   {
78      // Color.darkGray
79
return addTitleSeparator(title, TitledBorder.DEFAULT_JUSTIFICATION);
80   }
81
82
83   public JComponent addTitleSeparator(String JavaDoc title, int TitledBorderjustification)
84   {
85      // Color.darkGray
86
return addTitleSeparator(title,
87            UIManager.getColor("TitledBorder.titleColor"),
88            TitledBorderjustification, TitledBorder.DEFAULT_POSITION,
89            UIManager.getFont("Label.font").getSize(), 0);
90   }
91
92   /** you have to import javax.swing.border.*;
93    @param TitledBorderjustification TitledBorder.CENTER, ...
94    @param TitledBorderposition TitledBorder.DEFAULT_POSITION, ...
95      the color should be UIManager.getColor("TitledBorder.titleColor");
96   */

97   public JComponent addTitleSeparator(String JavaDoc 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        // this is for the last column of each row
139
constr.gridwidth = constr.REMAINDER;
140      }
141      else if(enforceLineBreakAfter)
142      {
143        constr.gridwidth = constr.REMAINDER;
144      }
145      else
146      {
147        // all the other columns
148
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        // default
159
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        // default behaviour, resize all but the first
170
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      // always reset, use only once when set to true
191
if(enforceLineBreakAfter)
192      {
193         // always reset, use only once when set to true
194
enforceLineBreakAfter = false;
195         actualCol = 0;
196      }
197      return comp;
198
199   }
200
201   boolean enforceLineBreakAfter = false;
202
203   /** force next added component to terminate the line
204   */

205   public void insertLineBreakAfterNextComponent()
206   {
207     enforceLineBreakAfter = true;
208     // constr.gridwidth = constr.REMAINDER;
209
// actualCol = 0;
210

211   }
212
213
214  /**
215   * Usage demo
216   */

217   public static void main( String JavaDoc[] 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   } // main
258

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.setSize(200,200);
284
frame.pack();
285     frame.setLocationRelativeTo(null);
286     frame.setVisible(true);
287   }
288
289   public static String JavaDoc getRandomString()
290   {
291      int len = (int) (Math.random()*30) + 1;
292      StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
293      for(int i=0; i<len; i++)
294      {
295        sb.append(""+(char)('a'+i));
296      }
297      return sb.toString();
298   }
299 } // GridLayout3
Popular Tags