Search code examples
javajtextfieldkeylistener

JKeyListener chars / auto complete


I want to make an auto completion textfield, which replaces the input with the first hit and selects the part which wasn't entered by the keyboard yet. To do so, I'll have to count the chars typed (or deleted). How can I filter if the KeyEvent is a printable Char (and not SHIFT f.e.)?


Solution

  • i prefer to do it with the KeyListener

    Well, a KeyListener is the wrong tool for the job.

    If you really want to write your own code then you should start with a DocumentListener. Every time text is added or removed from the text field you will be notified. Then you can implement your auto completion code.