Java Swing Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.00

javax.swing.JTextField and Related Classes

This section describes the javax.swing.JTextField class, the javax.swing.text.PlainDocument class, and the javax.swing.event.DocumentListener class.

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

Last update: 2009.

Sections in This Chapter

javax.swing.JTextField and Related Classes

ActionListener and DocumentListener

Dr. Herong Yang, updated in 2009
javax.swing.JTextField and Related Classes