how add a listener for jtexfield when it changing?

Mahdi_Nine picture Mahdi_Nine · Mar 26, 2011 · Viewed 55k times · Source

I have a JTextField. I want to invoke a function when the text in it is changed.

How do I do that?

Answer

JB Nizet picture JB Nizet · Mar 26, 2011

The appropriate listener in Java's swing to track changes in the text content of a JTextField is a DocumentListener, that you have to add to the document of the JTextField:

myTextField.getDocument().addDocumentListener(new DocumentListener() {
    // implement the methods
});