Herong's Tutorial Notes on Swing
Dr. Herong Yang, Version 3.05, 2006

Swing JTextField

This chapter describes:

  • JTextField class and its related classes.
  • Event listeners related to TextField class.

JTextField and Related Classes

javax.swing.JTextField - A Swing class representing a UI text field. Some interesting methods are:

  • JTextField(int) - Constructor to create a text field with the specified number of columns.
  • JTextField(String) - Constructor to create a text field with the specified string as the initial text.
  • addActionListener(ActionListener) - Method to add an action listener to this text field to handle action events. Pressing the "Enter" key in this field will trigger one action event.
  • getDocuement() - Method to return the document object that is used to hold the text of this field.
  • getText() - Method to return the text of this field as a string object.

javax.swing.text.PlainDocument - A Swing class representing a plain text document. PlainDocument is used as the default document for JTextField to hold its text. Method includes:

  • addDocumentListener(DocumentListener) - Method to add a document listener to this document.

javax.swing.event.DocumentListener - A Swing interface for the implementing class to handle document events. Method includes:

  • changeUpdate(DocumentEvent) - Method to be called when any attribute of the document is changed.
  • insertUpdate(DocumentEvent) - Method to be called when text is inserted into the document.
  • removeUpdate(DocumentEvent) - Method to be called when text is removed from the document

JTextField and Event Listeners

As you can see from the previous section, a text field can trigger action events directly. It can also trigger document events indirectly through its associated document. Here is a sample program to show you how and when those events are triggered:

/**
 * JTextFieldTest.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class JTextFieldTest {
   public static void main(String[] a) {
      JFrame f = new JFrame("Text Field Test");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      MyTextField t = new MyTextField(16);
      f.getContentPane().add(t,BorderLayout.CENTER);
      f.pack();
      f.setVisible(true);
   }
   private static class MyTextField extends JTextField 
      implements ActionListener, DocumentListener  {
      static int count = 0;
      public MyTextField(int l) {
         super(l);
         addActionListener(this);
         Document doc = this.getDocument();
         System.out.println("The document object: "+doc);
         doc.addDocumentListener(this);
      }
      public void actionPerformed(ActionEvent e) {
         count++;
         System.out.println(count+": Action performed - "+getText());
      }
      public void insertUpdate(DocumentEvent e) {
         count++;
         System.out.println(count+": Insert update - "+getText());
      }
      public void removeUpdate(DocumentEvent e) {
         count++;
         System.out.println(count+": Remove update - "+getText());
      }
      public void changedUpdate(DocumentEvent e) {
         count++;
         System.out.println(count+": Change update - "+getText());
      }
   }
}

Run this program and do the following in the text field:

  • Type "h".
  • Type "i".
  • Press the backspace key to remove "i".
  • Type "e".
  • Press the enter key.

You should get the following output in the console window:

The document object: javax.swing.text.PlainDocument@4e79f1
1: Insert update - h
2: Insert update - hi
3: Remove update - h
4: Insert update - he
5: Action performed - he

The output confirms that:

  • TextField class is using PlainDocument as the default document type.
  • ActionListener is called when the enter key is pressed.
  • changeUpdate() is not called.
Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes on Swing - Swing JTextField