View Javadoc

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.*;
9   
10  public class GridBagHelper {
11  
12      GridBagLayout gridbag;
13      Container container;
14      GridBagConstraints c;
15      int x = 0;
16      int y = 0;
17      int labelAlignment = SwingConstants.RIGHT;
18      double[] weights;
19  
20      public GridBagHelper(Container container, double[] weights) {
21          this.container = container;
22          this.weights = weights;
23  
24          gridbag = new GridBagLayout();
25          container.setLayout(gridbag);
26  
27          c = new GridBagConstraints();
28          c.insets = new Insets(2, 2, 2, 2);
29          c.anchor = GridBagConstraints.EAST;
30          c.fill = GridBagConstraints.HORIZONTAL;
31      }
32  
33      public void add(Component component) {
34          add(component, 1);
35      }
36  
37      public void add(Component component, int width) {
38          c.gridx = x;
39          c.gridy = y;
40          c.weightx = weights[x];
41          c.gridwidth = width;
42          gridbag.setConstraints(component, c);
43          container.add(component);
44          x += width;
45      }
46  
47      public void nextRow() {
48          y++;
49          x = 0;
50      }
51  
52      public void addLabel(String label) {
53          add(new JLabel(label, labelAlignment));
54      }
55  
56  }
57