Java TextFields, keyTyped Vs insertUpdate Events
It's been a while since I last posted, I have been spending a lot of my spare time on a java application where I came across a problem with the keyTyped() Event.
The task I was trying to achieve was to reproduce a similar feature to google suggest which was fairly straight forward simply using a Collection to store all my values and the Iterator to loop through each value checking against my search string which was retrieved using the getText() method and then matched against each value using the startsWith() method.
The problem I was having was the search feature was always a character behind therefore when you enter the first character all values are displayed, then after the second character was entered the results where filtered by the first character only.
The solution was to implement the DocumentListener class and use the insertUpdate() event to trigger my search method. Using this event to trigger my search method meant the getText() method is not called until the document is updated.
Note: Thanks to the guys over at sun forums who helped me solve this problem.