An example of text fields in Java

The following applet reads text from one TextField and capitalizes it in another TextField.

Capitalizer applet

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);
     Button b = new Button("Capitalize");

     // add the button to the layout

     // specify that action events sent by the
     // button or the input TextField should be handled 
     // by the same CapitalizerAction object
     CapitalizerAction ca = new CapitalizerAction(input, output);

     // notice that ActionEvents produced by output are ignored.


class CapitalizerAction implements ActionListener {

  private TextField in;
  private TextField out;

  public CapitalizerAction(TextField in, TextField out) { = in;
    this.out = out;

  public void actionPerformed(ActionEvent ae) {

    String s = in.getText();



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 | Cafe au Lait

Copyright 1997, 1999, 2001, 2002 Elliotte Rusty Harold
Last Modified February 20, 2002