import java.awt.*; // For FlowLayout, Font.
import javax.swing.*;

/** A TextField with an associated Label. 
 *
 *  Taken from Core Web Programming from 
 *  Prentice Hall and Sun Microsystems Press,
 *  http://www.corewebprogramming.com/.
 *  &copy; 2001 Marty Hall and Larry Brown;
 *  may be freely used or adapted.
 */

public class LabeledTextField extends JPanel {
  private JLabel label;
  private JTextField textField;
  
  public LabeledTextField(String labelString,
                          Font labelFont,
                          int textFieldSize,
                          Font textFont) {
    setLayout(new FlowLayout(FlowLayout.LEFT));
    label = new JLabel(labelString, JLabel.RIGHT);
    if (labelFont != null) {
      label.setFont(labelFont);
    }
    add(label);
    textField = new JTextField(textFieldSize);
    if (textFont != null) {
      textField.setFont(textFont);
    }
    add(textField);
  }

  public LabeledTextField(String labelString,
                          String textFieldString) {
    this(labelString, null, textFieldString,
         textFieldString.length(), null);
  }

  public LabeledTextField(String labelString,
                          int textFieldSize) {
    this(labelString, null, textFieldSize, null);
  }
  
  public LabeledTextField(String labelString,
                          Font labelFont,
                          String textFieldString,
                          int textFieldSize,
                          Font textFont) {
    this(labelString, labelFont,
         textFieldSize, textFont);
    textField.setText(textFieldString);
  }

  /** The Label at the left side of the LabeledTextField.
   *  To manipulate the Label, do:
   *  <PRE>
   *    LabeledTextField ltf = new LabeledTextField(...);
   *    ltf.getLabel().someLabelMethod(...);
   *  </PRE>
   */
  
  public JLabel getLabel() {
    return(label);
  }

  /** The TextField at the right side of the
   *  LabeledTextField.
   */
  
  public JTextField getTextField() {
    return(textField);
  }
}