]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/GuiGridLayout.java
Version 11, August 2010
[GpsPrune.git] / tim / prune / gui / GuiGridLayout.java
1 package tim.prune.gui;
2
3 import java.awt.GridBagConstraints;
4 import java.awt.GridBagLayout;
5 import java.awt.Insets;
6
7 import javax.swing.JComponent;
8 import javax.swing.JPanel;
9
10 /**
11  * Class to make it easier to use GridBagLayout
12  * for a two-column, non-equal-width layout
13  */
14 public class GuiGridLayout
15 {
16         private GridBagLayout _layout = null;
17         private GridBagConstraints _constraints = null;
18         private JPanel _panel = null;
19         private boolean _allLeft = false;
20         private int _x = 0;
21         private int _y = 0;
22
23         /**
24          * Constructor
25          * @param inPanel panel using layout
26          */
27         public GuiGridLayout(JPanel inPanel)
28         {
29                 this(inPanel, false);
30         }
31
32         /**
33          * Constructor
34          * @param inPanel panel using layout
35          * @param inAllLeft true to align all elements to left
36          */
37         public GuiGridLayout(JPanel inPanel, boolean inAllLeft)
38         {
39                 _panel = inPanel;
40                 _allLeft = inAllLeft;
41                 _layout = new GridBagLayout();
42                 _constraints = new GridBagConstraints();
43                 _constraints.weightx = 1.0;
44                 _constraints.weighty = 0.0;
45                 _constraints.ipadx = 10;
46                 _constraints.ipady = 1;
47                 _constraints.insets = new Insets(1, 5, 1, 5);
48                 // Apply layout to panel
49                 _panel.setLayout(_layout);
50         }
51
52         /**
53          * Add the given component to the grid
54          * @param inComponent component to add
55          */
56         public void add(JComponent inComponent)
57         {
58                 _constraints.gridx = _x;
59                 _constraints.gridy = _y;
60                 _constraints.weightx = (_x==0?0.5:1.0);
61                 // set anchor
62                 _constraints.anchor = ((_x == 0 && !_allLeft)?GridBagConstraints.LINE_END:GridBagConstraints.LINE_START);
63                 _layout.setConstraints(inComponent, _constraints);
64                 // add to panel
65                 _panel.add(inComponent);
66                 // work out next position
67                 _x++;
68                 if (_x > 1) {
69                         _x = 0;
70                         _y++;
71                 }
72         }
73 }