3 import java.awt.GridBagConstraints;
4 import java.awt.GridBagLayout;
5 import java.awt.Insets;
7 import javax.swing.JComponent;
8 import javax.swing.JPanel;
11 * Class to make it easier to use GridBagLayout for a non-equal-width layout
12 * Default is two columns but can handle more
14 public class GuiGridLayout
16 private GridBagLayout _layout = null;
17 private GridBagConstraints _constraints = null;
18 private JPanel _panel = null;
19 private int _numColumns = 0;
20 private double[] _colWeights = null;
21 private boolean[] _rightAligns = null;
27 * @param inPanel panel using layout
29 public GuiGridLayout(JPanel inPanel)
31 // Default is two columns, with more weight to the right-hand one; first column is right-aligned
32 this(inPanel, null, null);
37 * @param inPanel panel using layout
38 * @param inColumnWeights array of column weights
39 * @param inAlignRights array of booleans, true for right alignment, false for left
41 public GuiGridLayout(JPanel inPanel, double[] inColumnWeights, boolean[] inAlignRights)
44 _layout = new GridBagLayout();
45 _constraints = new GridBagConstraints();
46 _colWeights = inColumnWeights;
47 _rightAligns = inAlignRights;
48 if (_colWeights == null || _rightAligns == null || _colWeights.length != _rightAligns.length
49 || _colWeights.length < 2)
51 _colWeights = new double[] {0.5, 1.0};
52 _rightAligns = new boolean[] {true, false};
54 _numColumns = _colWeights.length;
55 _constraints.weightx = 1.0;
56 _constraints.weighty = 0.0;
57 _constraints.ipadx = 10;
58 _constraints.ipady = 1;
59 _constraints.insets = new Insets(1, 5, 1, 5);
60 // Apply layout to panel
61 _panel.setLayout(_layout);
65 * Add the given component to the grid
66 * @param inComponent component to add
68 public void add(JComponent inComponent)
70 _constraints.gridx = _x;
71 _constraints.gridy = _y;
72 _constraints.weightx = _colWeights[_x];
74 _constraints.anchor = (_rightAligns[_x]?GridBagConstraints.LINE_END:GridBagConstraints.LINE_START);
75 _layout.setConstraints(inComponent, _constraints);
77 _panel.add(inComponent);
78 // work out next position
80 if (_x >= _numColumns) {
86 * Go to the next row of the grid