简体   繁体   English

当我开始在文本字段中键入内容时,如何制作自动完成列表?

[英]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.. 谢谢..

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.. 用户输入两个或三个字母后,您可能要启动自动补全功能,否则列表可能会太长。

这里AutoCompleteComboBox / JFextField ,有两个类,一个是JComboBox ,第二个是JTextField ,请注意,自动完成功能需要两个类

I feeling generous as you really should google ... 我感到很慷慨,就像您真正应该使用Google一样...

As the user types, you'd need to query your DB with a like '<userInput>%' and return the results into a pulldown. 随着用户的键入,您需要使用like '<userInput>%'查询数据库,并将结果返回到下拉列表中。 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. 在没有数据库的情况下,适合此操作的数据结构称为Trie因为您可以遍历初始输入并显示所有后续单词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM