/* File: TextFieldTest.java * * An editable text field (i.e., a one-line text area) */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class TextFieldTest extends Applet { private Label usernameLabel, passwordLabel; private TextField username, password; private Panel usernamePanel, passwordPanel; public void init() { TextFieldHandler usernameHandler, passwordHandler; setBackground(Color.white); usernameLabel = new Label( "Username: ", Label.RIGHT ); username = new TextField( 20 ); usernameHandler = new TextFieldHandler( this ); usernameHandler.setPrefix( "Username is: " ); username.addActionListener( usernameHandler ); usernamePanel = new Panel(); usernamePanel.add( usernameLabel ); usernamePanel.add( username ); passwordLabel = new Label( "Password: ", Label.RIGHT ); password = new TextField( 20 ); password.setEchoChar( '*' ); passwordHandler = new TextFieldHandler( this ); passwordHandler.setPrefix( "Password received." ); password.addActionListener( passwordHandler ); passwordPanel = new Panel(); passwordPanel.add( passwordLabel ); passwordPanel.add( password ); // Instantiate a grid and add the panel: int rows = 2, cols = 1; setLayout( new GridLayout( rows, cols ) ); add( usernamePanel ); add( passwordPanel ); } } class TextFieldHandler implements ActionListener { private Applet applet; private String prefix; public TextFieldHandler ( Applet a ) { applet = a; prefix = ""; } public void setPrefix( String s ) { prefix = s; } public void actionPerformed( ActionEvent e ) { String text = e.getActionCommand(); if ( ! text.equals("") ) { TextField textfield = (TextField) e.getSource(); if ( textfield.echoCharIsSet() ) { applet.showStatus( prefix ); } else { applet.showStatus( prefix + text ); } } } }