An example of text fields in Java
The following applet reads text from one TextField and 
capitalizes it in another TextField.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class CapitalizeApplet extends Applet {
  private TextField input;
  private TextField output;
  
  public void init () {
   
     // Construct the TextFields
     this.input = new TextField(40);
     this.output = new TextField(40);
     this.output.setEditable(false);
     Button b = new Button("Capitalize");
     // add the button to the layout
     this.add(input);
     this.add(b);
     this.add(output);
     // specify that action events sent by the
     // button or the input TextField should be handled 
     // by the same CaptializerAction object
     CapitalizerAction ca = new CapitalizerAction(input, output);
     b.addActionListener(ca);
     this.input.addActionListener(ca);
     // notice that ActionEvents produced by output are ignored.
   
   }
}
class CapitalizerAction implements ActionListener {
  TextField in;
  TextField out;
  public CapitalizerAction(TextField in, TextField out) {
    this.in = in;
    this.out = out;
  }
  public void actionPerformed(ActionEvent ae) {
    String s = in.getText();
    out.setText(s.toUpperCase());
  }
}
In this program, a different pattern is used for handling events.
The constructor for the CapitalizerAction class
is used to pass in references to the different components
the actionPerformed() method affects.
Previous | Next | Top
Last Modified November 15, 1999
Copyright 1997, 1999 Elliotte Rusty Harold
elharo@metalab.unc.edu