Skip to main content

Autocomplete in a TextField

3 replies [Last post]
pkonrad
Offline
Joined: 2006-04-20

Hi everybody,

I would like to add autocomplete functionality to some Textfields. It should work like the one in windows search textfield. I mean that particularly a popup with the matches should appear under the textfield.

My first attemts to do so using org.jdesktop.swingx.autocomplete failed.
Here is my approach:
JTextField textfield = new JTextField();
DefaultListModel model = new DefaultListModel();
model.addElement("aaa");
model.addElement("aaaxxx");
model.addElement("bbb");
ListAdaptor adaptor = new ListAdaptor(new JList(model), textfield);
AutoCompleteDocument autoDoc = new AutoCompleteDocument(adaptor, false);
AutoCompleteDecorator.decorate(textfield, autoDoc, adaptor);

The first thing is that the provided JList wasn't used for the popup as my naive expectation was. So there was no list with matches displayed.
Is it possible to implement the desired functionality using the org.jdesktop.swingx.autocomplete package?
If yes, what would be the best way to do it? Implement my own AbstractAutoCompleteAdaptor?

The second thing is that I was not able to provide a value which is the beginning of an already existing value (in my example it would be "aa" what is the beginning of "aaa"). On loosing focus the value gets automatically autocompleted :-)
I assume its a bug.

Thanks,
Philipp
P.S. BTW SwingLabs is a great lib!

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Kleopatra

Hi Philipp,

no answer - just a note: Thomas (the caring father of the autocomplete
support :-) currently is on vacation, so please don't be surprised if
there's no timely response.

Thanks
Jeanette

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net

pkonrad
Offline
Joined: 2006-04-20

Hi Jeanette,

thank you for the information.

pkonrad
Offline
Joined: 2006-04-20

And another two minor points:

- the backspace key gets overloaded and selects text instead of deleting it. I think this behaviour is perfect for strict matching but it's annoying for non strict. But it might be just a matter of personal favor. What is your opinion?

- can the interface be simplified by removing the need to provide an adaptor on calling of AutoCompleteDecorator.decorate(JTextComponent textComponent, AutoCompleteDocument document, AbstractAutoCompleteAdaptor adaptor)? The adaptor is anyway used by creation of the AutoCompleteDocument.