1  /*  File:  TextFieldTest.java
  2   *
  3   *  An editable text field (i.e., a one-line text area)
  4   */
  5  
  6  import java.applet.Applet;
  7  import java.awt.*;
  8  import java.awt.event.*;
  9  
 10  public class TextFieldTest extends Applet {
 11  
 12    private Label usernameLabel, passwordLabel;
 13    private TextField username, password;
 14    private Panel usernamePanel, passwordPanel;
 15  
 16    public void init() {
 17      TextFieldHandler usernameHandler, passwordHandler;
 18      
 19      setBackground(Color.white);
 20      
 21      usernameLabel = new Label( "Username:  ", Label.RIGHT );
 22      
 23      username = new TextField( 20 );
 24      usernameHandler = new TextFieldHandler( this );
 25      usernameHandler.setPrefix( "Username is: " );
 26      username.addActionListener( usernameHandler );
 27      
 28      usernamePanel = new Panel();
 29      usernamePanel.add( usernameLabel );
 30      usernamePanel.add( username );
 31      
 32      passwordLabel = new Label( "Password:  ", Label.RIGHT );
 33      
 34      password = new TextField( 20 );
 35      password.setEchoChar( '*' );
 36      passwordHandler = new TextFieldHandler( this );
 37      passwordHandler.setPrefix( "Password received." );
 38      password.addActionListener( passwordHandler );
 39      
 40      passwordPanel = new Panel();
 41      passwordPanel.add( passwordLabel );
 42      passwordPanel.add( password );
 43      
 44      // Instantiate a grid and add the panel:
 45      int rows = 2, cols = 1;
 46      setLayout( new GridLayout( rows, cols ) );
 47      add( usernamePanel ); add( passwordPanel );
 48    }
 49  
 50  }
 51  
 52  class TextFieldHandler implements ActionListener {
 53  
 54    private Applet applet;
 55    private String prefix;
 56    
 57    public TextFieldHandler ( Applet a ) {
 58      applet = a; prefix = "";
 59    }
 60    
 61    public void setPrefix( String s ) { prefix = s; }
 62  
 63    public void actionPerformed( ActionEvent e ) {
 64      String text = e.getActionCommand();
 65      if ( ! text.equals("") ) {
 66        TextField textfield = (TextField) e.getSource();
 67        if ( textfield.echoCharIsSet() ) {
 68          applet.showStatus( prefix );
 69        } else {
 70          applet.showStatus( prefix + text );
 71        }
 72      }
 73    }
 74    
 75  }