You are responding to keyPressed, which happens when the user presses a key (duh!). But the input to the text field doesn't happen until the key is released and a valid character has been typed - so that's why you don't see the latest char in the getText. (Ps Even worse if it's (eg) an upper case character, because you'll get keyPressed events for the shift key AND the letter key before the text field is updated.)
Much better to implement your method in keyTyped, which is triggered when a character has been typed, and which automatically handles things like the shift key or alt-gr.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
you have to add DocumentListener for listening any of changes in JTextComponents
this code for ??? AutoComplete ??? JTextField have got two big issues
1) most important rs (ResultSet) and stmt (Statement) must be closed into finally block, otherwise these Object stay in RAM until current application ended
2) your GUI waiting for returns from SQL server, during this long task isn't GUI accesible and freeze, load whole Resultset once time and implement AutoComplete JComboBox / JTextField
mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224