In keeping with the Model-View-Controller architecture, we don't exactly
add a listener to the JTextComponent itself, we add it to its model.
With text components, this is called a Document interface.
So, we need to get the Document object from the JTextField
object,
and add the DocumentListener object to it. Assume we have
a document listener called listener. We can then add it
like so:
The DocumentListener interface has three methods we can use:
void insertUpdate(DocumentEvent e) is called when the text in the field
is updated. You can put validation logic here.
void removeUpdate(DocumentEvent e) is called when the text is removed.
You could restore a default value here, or inform the user that the field
is required.
void changeUpdate(DocumentEvent e) is called when other changes occur.
This does not apply to a JTextField.