I want to create a auto-suggest text box which would query the database at each key release event.
That part is easy, but I want to give nice visual to it. Something similar to the auto-suggest text box we see in websites like searching in Facebook.
How to make such an interface ?
A naive idea would be to have a JList placed just below the text box and set it visible with the results in it on finding one.
Any better idea or a standard way of doing it?
@syb0rg’s answer is easier, as it uses an 3rd party library.
However I used an alternate approach:
It uses a custom class called
AutoSuggestorwhich accepts aJTextField, itsWindowanArrayList<String>of words to check typed words against, a background color and text color, and suggestion focus colour as well as an opacity value. By passingJTextFieldreference aDocumentListeneris added which will do the work of checking what word is typed and whether to display suggestions or not and if so what suggestions to display. When a word is typed theDocumentListenerwill firewordTyped(String wordTyped)method with the current word being typed or (at least how much ever of the word has been typed), inwordTyped(..)the word will be checked against those in theAutoSuggestors classes dictionary which is a basicArrayListofStringthis can be set on the fly as seen in the below example:(For now you will have to use mouse and click the word you want to be auto completed, or use DOWN to transverse suggestions and the textfield and ENTER to select suggestion when traversing using down key. I have not yet implemented UP yet):
As it stands the only possible needed additions IMO is:
If there are any kinks lemme know I’ll see what I can do. But Seems to be running fine (touch wood).