1  /* An editable text field (i.e., a one-line text area) */
  2  
  3  import java.awt.*;
  4  
  5  public class TextFieldTest extends java.applet.Applet {
  6  
  7    private Label label, outLabel;
  8    private TextField name;
  9  
 10    public void init() {
 11      setBackground(Color.white);
 12      
 13      label = new Label("Name:  ", Label.RIGHT);
 14      label.setBackground(Color.white);
 15      name = new TextField(40);
 16      name.setBackground(Color.white);
 17      // Build a string of spaces:
 18      String spaces = "";
 19      for ( int i = 0; i < 80; i++, spaces += " " );
 20      outLabel = new Label(spaces, Label.CENTER);
 21      outLabel.setBackground(Color.white);
 22      
 23      add(label); add(name); add(outLabel);
 24    }
 25  
 26    // Pressing return in a text field generates an action event:
 27    public boolean action(Event event, Object arg) {
 28      if ( event.target instanceof TextField ) {
 29        // Handling TextField event:
 30        if ( event.target == name ) {
 31          String s, t = name.getText();
 32          if ( t.equals("") ) s = "What is your name?";
 33          else s = "Hi " + t + "!";
 34          outLabel.setText(s);
 35        } else {
 36          return super.action(event, arg);
 37        }
 38        return true;
 39      }
 40      return super.action(event, arg);
 41    }
 42    
 43  }