]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/gui/GuiGridLayout.java
Version 9, February 2010
[GpsPrune.git] / tim / prune / gui / GuiGridLayout.java
diff --git a/tim/prune/gui/GuiGridLayout.java b/tim/prune/gui/GuiGridLayout.java
new file mode 100644 (file)
index 0000000..cb3572b
--- /dev/null
@@ -0,0 +1,61 @@
+package tim.prune.gui;
+
+import java.awt.GridBagConstraints;
+import java.awt.GridBagLayout;
+import java.awt.Insets;
+
+import javax.swing.JComponent;
+import javax.swing.JPanel;
+
+/**
+ * Class to make it easier to use GridBagLayout
+ * for a two-column, non-equal-width layout
+ */
+public class GuiGridLayout
+{
+       private GridBagLayout _layout = null;
+       private GridBagConstraints _constraints = null;
+       private JPanel _panel = null;
+       private int _x = 0;
+       private int _y = 0;
+
+       /**
+        * Constructor
+        * @param inPanel panel using layout
+        */
+       public GuiGridLayout(JPanel inPanel)
+       {
+               _panel = inPanel;
+               _layout = new GridBagLayout();
+               _constraints = new GridBagConstraints();
+               _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);
+       }
+
+       /**
+        * Add the given component to the grid
+        * @param inComponent component to add
+        */
+       public void add(JComponent inComponent)
+       {
+               _constraints.gridx = _x;
+               _constraints.gridy = _y;
+               _constraints.weightx = (_x==0?0.5:1.0);
+               // set anchor
+               _constraints.anchor = (_x == 0?GridBagConstraints.LINE_END:GridBagConstraints.LINE_START);
+               _layout.setConstraints(inComponent, _constraints);
+               // add to panel
+               _panel.add(inComponent);
+               // work out next position
+               _x++;
+               if (_x > 1) {
+                       _x = 0;
+                       _y++;
+               }
+       }
+}