Search code examples
javaswingjtextfield

how add a listener for jtexfield when it changing?


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

How do I do that?


Solution

  • 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
    });