KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > cpd > GridBagHelper


1 /**
2  * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3  */

4 // stolen from XPath Explorer (http://www.xpathexplorer.com)
5
package net.sourceforge.pmd.cpd;
6
7 import javax.swing.*;
8 import java.awt.Component JavaDoc;
9 import java.awt.Container JavaDoc;
10 import java.awt.GridBagConstraints JavaDoc;
11 import java.awt.GridBagLayout JavaDoc;
12 import java.awt.Insets JavaDoc;
13
14 public class GridBagHelper {
15
16     GridBagLayout JavaDoc gridbag;
17     Container JavaDoc container;
18     GridBagConstraints JavaDoc c;
19     int x = 0;
20     int y = 0;
21     int labelAlignment = SwingConstants.RIGHT;
22     double[] weights;
23
24     public GridBagHelper(Container JavaDoc container, double[] weights) {
25         this.container = container;
26         this.weights = weights;
27
28         gridbag = new GridBagLayout JavaDoc();
29         container.setLayout(gridbag);
30
31         c = new GridBagConstraints JavaDoc();
32         c.insets = new Insets JavaDoc(2, 2, 2, 2);
33         c.anchor = GridBagConstraints.EAST;
34         c.fill = GridBagConstraints.HORIZONTAL;
35     }
36
37     public void add(Component JavaDoc component) {
38         add(component, 1);
39     }
40
41     public void add(Component JavaDoc component, int width) {
42         c.gridx = x;
43         c.gridy = y;
44         c.weightx = weights[x];
45         c.gridwidth = width;
46         gridbag.setConstraints(component, c);
47         container.add(component);
48         x += width;
49     }
50
51     public void nextRow() {
52         y++;
53         x = 0;
54     }
55
56     public void addLabel(String JavaDoc label) {
57         add(new JLabel(label, labelAlignment));
58     }
59
60 }
61
62
Popular Tags