
import java.awt.*;

/**
  * Sets up a panel that is a 2-column grid with field labels in the first
  * column and AWT components in the second column.
  * @author     Paul Hyde
  */
public class LabelCompPanel extends Panel {

    GridBagLayout gbl = new GridBagLayout();

    GridBagConstraints labelGBC = new GridBagConstraints();
    GridBagConstraints compGBC = new GridBagConstraints();
    
    /**
      * Create a panel with defaults that labels are right/top justified 
      * and components are left/top justtified in a GridBagLayout.
      */
    public LabelCompPanel () {
        // these panels use the Grid Bag layout
        setLayout(gbl);

        // labels are top and right justified in cell
        labelGBC.anchor = GridBagConstraints.NORTHEAST;
        labelGBC.insets = new Insets(2, 2, 2, 2);

        // components are top and left justified in cell, last element in row
        compGBC.anchor = GridBagConstraints.NORTHWEST;
        compGBC.gridwidth = GridBagConstraints.REMAINDER;
        compGBC.insets = new Insets(2, 2, 2, 2);
    }

    /**
      * Adds a label and a generic component to this panel
      */
    public LabelCompPair addLabelComp(Label l, Component c) {
        // tell the layout how each of these should be placed
        gbl.setConstraints(l, labelGBC);
        gbl.setConstraints(c, compGBC);

        // add components to panel in proper order
        add(l);
        add(c);

        // allow caller to access newly created components
        return new LabelCompPair(l, c);
    }

    /**
      * Adds a label and textfield pair to this panel
      */
    public LabelCompPair addTextField(String labelText, int fieldLen) {
        Label l = new Label(labelText);
        TextField tf = new TextField(fieldLen);
        return addLabelComp(l, tf);
    }
}
