papski
papski

Reputation: 1281

How to make an auto-complete list when I start to type in the textfield?

I have a textfield and if I want to write something to the field, it will show me the list of possible options regarding to that letter and I think this is called an auto complete.

Could someone give me an idea or a sample on how to do it?

Thanks..

Upvotes: 1

Views: 4174

Answers (3)

mKorbel
mKorbel

Reputation: 109815

look here is AutoCompleteComboBox / JFextField, and there are two classes one for JComboBox, second for JTextField, notice auto-complete functionality requires both classes for that

Upvotes: 2

Andreas Dolk
Andreas Dolk

Reputation: 114767

Take a combo box and listen to all changes in the textfield. On every event, read the actual content and query your source list for possible matches. Then use the result to populate the associated list.

You may want to start autocompletion once the user has entered two or three letters, otherwise the list may get too long..

Upvotes: 4

Brian Roach
Brian Roach

Reputation: 76898

I feeling generous as you really should google ...

As the user types, you'd need to query your DB with a like '<userInput>%' and return the results into a pulldown. You probably want to wait for a pause in the user's typing so as not to hammer your DB.

In the absence of a database, a data structure that would work well for this is called a Trie as you can traverse it past the initial input and present all the subsequent words.

Upvotes: 1

Related Questions