An example of text fields in Java

An alternate pattern uses an inner class so the private fields can be accessed directly.

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 CapitalizerAction object
     CapitalizerAction ca = new CapitalizerAction();
     b.addActionListener(ca);
     this.input.addActionListener(ca);

     // notice that ActionEvents produced by output are ignored.
   
   }

  class CapitalizerAction implements ActionListener {

    public void actionPerformed(ActionEvent ae) {

      String s = input.getText();
      output.setText(s.toUpperCase());

    }

  }


}

Previous | Next | Top | Cafe au Lait

Copyright 1997, 1999, 2001, 2002 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified November 1, 2002