Class GridBagBuilder


  • public class GridBagBuilder
    extends java.lang.Object
    A form builder that simplifies usage of GridBagLayout. The initial constraints are as follows
    • (gridx, gridy) = (0, 0)
    • fill = HORIZONTAL
    • weightx, weighty = 1.0
    • anchor = WEST
    The constraints are updated using a GridBagFormula when each component is added.

    By default, the following components are automatically wrapped in a JScrollPane. Automatic wrapping of components in JScrollPane can be enabled/disabled using useScrollPane(Class) and noUseScrollPane(Class)

    • JTable
    • JList
    • JTextArea
    • Constructor Summary

      Constructors 
      Constructor Description
      GridBagBuilder​(java.awt.Container container, java.util.ResourceBundle bundle, java.lang.String resourcePrefix)
      Create a new builder using 2 columns and the default GridBagFormulas.
      GridBagBuilder​(java.awt.Container container, java.util.ResourceBundle bundle, java.lang.String resourcePrefix, int columns)
      Create a new builder using the default GridBagFormulas.
      GridBagBuilder​(java.awt.Container container, java.util.ResourceBundle bundle, java.lang.String resourcePrefix, int columns, java.util.Map<java.lang.Class<?>,​GridBagFormula> fieldConstraints)
      Create a new builder using specific GridBagFormulas.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <T extends javax.swing.JComponent>
      T
      append​(java.lang.String labelKey, T field)
      Append a labeled component to the form.
      <T extends javax.swing.JComponent>
      T
      append​(java.lang.String labelKey, T field, GridBagFormula formula)
      Append a labeled component to the form.
      void append​(javax.swing.JRadioButton... buttons)
      Append a group of radio buttons in a horizontal box.
      <T extends javax.swing.JComponent>
      T
      append​(T field)
      Append an unlabeled component to the form.
      <T extends javax.swing.JComponent>
      T
      append​(T field, GridBagFormula formula)
      Append an unlabeled component to the form.
      javax.swing.JLabel getLastLabel()  
      GridBagBuilder insets​(int top, int left, int bottom, int right)
      Set the insets for the next component.
      void nextCell()
      Move to the next cell.
      GridBagBuilder noUseScrollPane​(java.lang.Class<? extends javax.swing.JComponent> componentClass)
      Disable automatic wrapping of instances of the class in a JScrollPane.
      <T extends javax.swing.JComponent>
      T
      overlay​(T field)
      Add a component in the same location as the previous component.
      GridBagBuilder relatedGap()
      Use the horizontal and vertical RELATED_GAP for the next component.
      GridBagBuilder setConstraints​(java.lang.Class<?> componentClass, GridBagFormula constraints)  
      GridBagBuilder unrelatedVerticalGap()
      Use the vertical UNRELATED_GAP and horizontal RELATED_GAP for the next component.
      GridBagBuilder useScrollPane​(java.lang.Class<? extends javax.swing.JComponent> componentClass)
      Automatically wrap instances of the class in a JScrollPane.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • GridBagBuilder

        public GridBagBuilder​(java.awt.Container container,
                              java.util.ResourceBundle bundle,
                              java.lang.String resourcePrefix)
        Create a new builder using 2 columns and the default GridBagFormulas.
        Parameters:
        container - the form container
        bundle - provides label text
        resourcePrefix - prefix for resource bundle keys
        See Also:
        FormElement
      • GridBagBuilder

        public GridBagBuilder​(java.awt.Container container,
                              java.util.ResourceBundle bundle,
                              java.lang.String resourcePrefix,
                              int columns)
        Create a new builder using the default GridBagFormulas.
        Parameters:
        container - the form container
        bundle - provides label text
        resourcePrefix - prefix for resource bundle keys
        columns - the number of grid columns
      • GridBagBuilder

        public GridBagBuilder​(java.awt.Container container,
                              java.util.ResourceBundle bundle,
                              java.lang.String resourcePrefix,
                              int columns,
                              java.util.Map<java.lang.Class<?>,​GridBagFormula> fieldConstraints)
        Create a new builder using specific GridBagFormulas.
        Parameters:
        container - the form container
        bundle - provides label text
        resourcePrefix - prefix for resource bundle keys
        columns - the number of grid columns
        fieldConstraints - map of component class to GridBagFormula
    • Method Detail

      • useScrollPane

        public GridBagBuilder useScrollPane​(java.lang.Class<? extends javax.swing.JComponent> componentClass)
        Automatically wrap instances of the class in a JScrollPane.
      • noUseScrollPane

        public GridBagBuilder noUseScrollPane​(java.lang.Class<? extends javax.swing.JComponent> componentClass)
        Disable automatic wrapping of instances of the class in a JScrollPane.
      • getLastLabel

        public javax.swing.JLabel getLastLabel()
        Returns:
        the most recently created field label
      • relatedGap

        public GridBagBuilder relatedGap()
        Use the horizontal and vertical RELATED_GAP for the next component.
        Returns:
        this builder
      • insets

        public GridBagBuilder insets​(int top,
                                     int left,
                                     int bottom,
                                     int right)
        Set the insets for the next component.
        Returns:
        this builder
      • append

        public <T extends javax.swing.JComponent> T append​(java.lang.String labelKey,
                                                           T field)
        Append a labeled component to the form.
        Type Parameters:
        T - the class of the component
        Parameters:
        labelKey - the resource key for the label
        field - the component
        Returns:
        the component
      • append

        public <T extends javax.swing.JComponent> T append​(java.lang.String labelKey,
                                                           T field,
                                                           GridBagFormula formula)
        Append a labeled component to the form.
        Type Parameters:
        T - the class of the component
        Parameters:
        labelKey - the resource key for the label
        field - the component
        formula - the constraints calculator for the component
        Returns:
        the component
      • append

        public <T extends javax.swing.JComponent> T append​(T field)
        Append an unlabeled component to the form.
        Type Parameters:
        T - the class of the component
        Parameters:
        field - the component
        Returns:
        the component
      • append

        public <T extends javax.swing.JComponent> T append​(T field,
                                                           GridBagFormula formula)
        Append an unlabeled component to the form.
        Type Parameters:
        T - the class of the component
        Parameters:
        field - the component
        formula - the constraints calculator for the component
        Returns:
        the component
      • overlay

        public <T extends javax.swing.JComponent> T overlay​(T field)
        Add a component in the same location as the previous component.
        Returns:
        the component
      • append

        public void append​(javax.swing.JRadioButton... buttons)
        Append a group of radio buttons in a horizontal box.
        Parameters:
        buttons - the buttons to append
      • nextCell

        public void nextCell()
        Move to the next cell. If there are columns left in the row then moves to the next column. Otherwise, moves to the first column of the next row.