1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
package tim.prune.gui;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* Class to make it easier to use GridBagLayout for a non-equal-width layout
* Default is two columns but can handle more
*/
public class GuiGridLayout
{
private final GridBagLayout _layout;
private final GridBagConstraints _constraints;
private final JPanel _panel;
private final int _numColumns;
private double[] _colWeights = null;
private boolean[] _rightAligns = null;
private int _x = 0;
private int _y = 0;
/**
* Constructor
* @param inPanel panel using layout
*/
public GuiGridLayout(JPanel inPanel)
{
// Default is two columns, with more weight to the right-hand one; first column is right-aligned
this(inPanel, null, null);
}
/**
* Constructor
* @param inPanel panel using layout
* @param inColumnWeights array of column weights
* @param inAlignRights array of booleans, true for right alignment, false for left
*/
public GuiGridLayout(JPanel inPanel, double[] inColumnWeights, boolean[] inAlignRights)
{
_panel = inPanel;
_layout = new GridBagLayout();
_constraints = new GridBagConstraints();
_colWeights = inColumnWeights;
_rightAligns = inAlignRights;
if (_colWeights == null || _rightAligns == null || _colWeights.length != _rightAligns.length
|| _colWeights.length < 2)
{
_colWeights = new double[] {0.5, 1.0};
_rightAligns = new boolean[] {true, false};
}
_numColumns = _colWeights.length;
_constraints.weightx = 1.0;
_constraints.weighty = 0.0;
_constraints.ipadx = 10;
_constraints.ipady = 1;
_constraints.insets = new Insets(1, 5, 1, 5);
// Apply layout to panel
_panel.setLayout(_layout);
}
/**
* @param inPadding y padding of grid cells (default 1)
*/
public void setYPadding(int inPadding) {
_constraints.ipady = inPadding;
}
/**
* Add the given component to the grid
* @param inComponent component to add
*/
public void add(JComponent inComponent) {
add(inComponent, 1, false);
}
/**
* Add the given component to the grid
* @param inComponent component to add
* @param inFillWidth true to fill width of column, default false
*/
public void add(JComponent inComponent, boolean inFillWidth) {
add(inComponent, 1, inFillWidth);
}
/**
* Add the given component to the grid
* @param inComponent component to add
* @param inColspan number of columns to span (normally 1)
* @param inFillWidth true to fill width of column, default false
*/
public void add(JComponent inComponent, int inColspan, boolean inFillWidth)
{
_constraints.gridx = _x;
_constraints.gridy = _y;
_constraints.weightx = _colWeights[_x];
_constraints.gridwidth = inColspan;
// set anchor
_constraints.anchor = (_rightAligns[_x]?GridBagConstraints.LINE_END:GridBagConstraints.LINE_START);
_constraints.fill = (inFillWidth ? GridBagConstraints.HORIZONTAL: GridBagConstraints.NONE);
_layout.setConstraints(inComponent, _constraints);
// add to panel
_panel.add(inComponent);
// work out next position
_x += inColspan;
if (_x >= _numColumns) {
nextRow();
}
}
/**
* Go to the next row of the grid
*/
public void nextRow()
{
_x = 0;
_y++;
}
public void addVerticalGap(int inPixels)
{
JLabel invisibleLabel = new JLabel();
invisibleLabel.setPreferredSize(new Dimension(10, inPixels));
add(invisibleLabel);
nextRow();
}
}
|