The WizardTextBoxPanel component.

Component Java
Build Wizards Quickly Using a Swing-Based Wizard Framework
Chang Sau Sheong
Listing 5. The WizardTextBoxPanel component.


. . .

public class WizardTextBoxPanel extends JPanel implements
        WizardComponent {

  JLabel label = new JLabel();
  JTextField textBox = new JTextField();
  Hashtable container = new Hashtable();


  public WizardTextBoxPanel(String labelText, int textLength) {
    label = new JLabel(labelText);
    textBox = new JTextField(textLength);
    setLayout(new BorderLayout());
    add(label, BorderLayout.WEST);
    add(textBox, BorderLayout.EAST);
}

  public WizardTextBoxPanel(Hashtable textBoxList) {
    if (!textBoxList.isEmpty()) {
      JPanel leftPane = new JPanel();
      JPanel rightPane = new JPanel();

      leftPane.setLayout(new
       BoxLayout(leftPane,BoxLayout.Y_AXIS));
       rightPane.setLayout(new
       BoxLayout(rightPane,BoxLayout.Y_AXIS));

       Enumeration textBoxes = textBoxList.keys();
       while (textBoxes.hasMoreElements()) {
	         String labelName = (String)textBoxes.nextElement();
	         label = new JLabel(labelName);
	         Integer textBoxLength =
	                  (Integer)textBoxList.get(labelName);
           textBox = new JTextField(textBoxLength.intValue());
	          leftPane.add(label);
	          rightPane.add(textBox);
	          container.put(labelName, textBox);
          }
	       add(leftPane, BorderLayout.WEST);
	       add(rightPane, BorderLayout.EAST);
    }
  }

  public String getComponentData() throws WizardException{
	       return textBox.getText();
  }

  public String getComponentData(String textBoxName)
                            throws WizardException {
	       JTextField textField =
	       (JTextField)container.get(textBoxName);
	       return textField.getText();
  }

  public Hashtable getAllComponentData() throws WizardException {
return null;
  }
}

. . .

About the Author

Chang Sau Sheong is vice president of product engineering at elipva Ltd. (formerly known as sstarfire.com). He can be contacted at [email protected].