Search code examples
javaswingjtextfield

Typing into a JTextField enables a button. Is it wrong to use a KeyListener?


I have a button called "save changes" that will save any changes if any changes are detected in a JTextField component. For now, I assume if the user types anything, then the content has changed.

I am using a KeyListener, but from this question it sounds like using anything other than an ActionListener is wrong?


Solution

  • You can add a DocumentListener to the document of the JTextField. ActionListener gets called only when the used presses enter. The advantage of using a document listener is that you can also detect changes made by other means than just by typing.