Unless you apply particular behavior to your list, you could try another way, by adding a combobox as editor of this table. Check DefaultCellEditor for an example of it.
If you modify the content of the combobox when it's called, with the correct values, it will match your need, I think. For this, you will need to make your own CellEditor, most likely by implementing TableCellEditor, so that you will be able to change the values from it depending on when it is called (in the method getTableCellEditorComponent()).
Edit: About the KeyListener problem you are talking about in comment, you have to think that a Key event is sent before the actual text is updated in the JTextField. So it is normal that your call to getText()
returns the value without the new character.
However, since this is a KeyEvent, you have access to the typed character, with evt.getKeyChar()
directly, or evt.getKeyCode()
, to check if this is actually a letter which was typed. With these methods, you can know the complete "text" that you need.
Also, from the video in your comment, it seems that you want a list which is in fact adapting according to what the user started typing, and restraining choice according to what was already entered.
If you are able (and allowed) to use extra libraries, I would recommend you to have a look at SwingX components (http://swinglabs.org/). This library proposes in general plenty of useful components to use in swing interfaces. There is a demo on their site, though it seems to not be available at this hour, maybe a later day.
In their package "autocomplete", you will be able to find a class named "AutoCompleteDecorator", and other useful ones, which will allow you to improve your Combobox editor, so that it will try to complete the user input itself, and scroll to the good position in the list (I think it can also filter the list, the exact behavior you want, but I'm not fully sure). If you can use this, it would actually save you the hassle of taking care of the caret events yourself, as well as updating the list, as it would most likely do it for you.
You can download the .Jar and the javadoc on their site. Here is a copy of the javadoc from another site, for the autocomplete package, though it could have changed in the meantime, but it will give you an idea.