]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/GuiGridLayout.java
cb3572b7b2dcdcfe14ead8c516af5b58d827a722
[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 int _x = 0;
20         private int _y = 0;
21
22         /**
23          * Constructor
24          * @param inPanel panel using layout
25          */
26         public GuiGridLayout(JPanel inPanel)
27         {
28                 _panel = inPanel;
29                 _layout = new GridBagLayout();
30                 _constraints = new GridBagConstraints();
31                 _constraints.weightx = 1.0;
32                 _constraints.weighty = 0.0;
33                 _constraints.ipadx = 10;
34                 _constraints.ipady = 1;
35                 _constraints.insets = new Insets(1, 5, 1, 5);
36                 // Apply layout to panel
37                 _panel.setLayout(_layout);
38         }
39
40         /**
41          * Add the given component to the grid
42          * @param inComponent component to add
43          */
44         public void add(JComponent inComponent)
45         {
46                 _constraints.gridx = _x;
47                 _constraints.gridy = _y;
48                 _constraints.weightx = (_x==0?0.5:1.0);
49                 // set anchor
50                 _constraints.anchor = (_x == 0?GridBagConstraints.LINE_END:GridBagConstraints.LINE_START);
51                 _layout.setConstraints(inComponent, _constraints);
52                 // add to panel
53                 _panel.add(inComponent);
54                 // work out next position
55                 _x++;
56                 if (_x > 1) {
57                         _x = 0;
58                         _y++;
59                 }
60         }
61 }