Java Swing 中的自动完成文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1861921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:17:41  来源:igfitidea点击:

Auto complete textbox in Java Swing

javaswing

提问by Sudar

Is there any good ready made Java Swing component that can be used to implement auto complete textbox?

是否有任何现成的 Java Swing 组件可用于实现自动完成文本框?

采纳答案by Samuel Sj?berg

Here's an example on autocompletion without a drop down selection.It will only autocomplete for you when a unique match is found. The completion is auto inserted into the text field and selected. Kind of similar as to how Safari's address field works.

这是一个没有下拉选择的自动完成示例。只有在找到唯一匹配项时,它才会为您自动完成。完成自动插入到文本字段中并被选中。有点类似于 Safari 的地址字段的工作方式。

The solution could be expanded to provide a list of options, however that was not part of my requirements when doing this.

可以扩展该解决方案以提供选项列表,但是在执行此操作时这不是我的要求的一部分。

UPDATE

更新

I lost the domain with the original code. It can now be accessed on github: https://github.com/sasjo/autocomplete

我丢失了带有原始代码的域。现在可以在 github 上访问:https: //github.com/sasjo/autocomplete

回答by monksy

There is a claim that JIDE-OSShas the feature that you are requesting. That is what this forum claims, however I have not tried it.

有人声称JIDE-OSS具有您所要求的功能。这就是本论坛所声称的,但是我还没有尝试过。

回答by miku

A little dated, but with a Java Web Start File attached, which actually works: http://www.jroller.com/santhosh/date/20050620.

有点过时,但附加了一个 Java Web Start 文件,它实际上有效:http: //www.jroller.com/santhosh/date/20050620

回答by William Dutton

I had some good success by using a combo box, setting the layout manager of the combo box to be border layout and then sticking in an editable JTextField. The user edits the text field which you can add a document listener to and trigger the underlying combo box to show the dropdown showing current suggestions. You can listen to the combo box for item selections and update the JTextField with the selected item. You can open the combo box using:

通过使用组合框,将组合框的布局管理器设置为边框布局,然后粘贴在可编辑的 JTextField 中,我取得了一些不错的成功。用户编辑文本字段,您可以向其中添加文档侦听器并触发底层组合框以显示显示当前建议的下拉列表。您可以收听项目选择的组合框,并使用所选项目更新 JTextField。您可以使用以下方法打开组合框:

getUI().setPopupVisible(JComboBox c, boolean v)

To prevent accidental opening of the combo box from clicking the edge ensure you give it a zero size empty border.

为防止意外打开组合框点击边缘,请确保为其提供零大小的空白边框。

回答by Charles Cyril Russell

I hated the idea of a third-party solution, so I was always looking a way far from it. I've settle with the JTextField and a hidden JComboBox solution when I was looking with the same idea. Lately, I found something new, and seems like nobody's looking at it as alternative solution. Doing like, textfield and a clean JPopUpMenu. . .

我讨厌第三方解决方案的想法,所以我总是离它很远。当我以相同的想法寻找时,我已经解决了 JTextField 和隐藏的 JComboBox 解决方案。最近,我发现了一些新东西,似乎没有人将其视为替代解决方案。这样做,文本字段和一个干净的 JPopUpMenu。. .

https://www.geeksforgeeks.org/java-swing-jpopupmenu/

https://www.geeksforgeeks.org/java-swing-jpopupmenu/

Maybe it could help, for others at least. . .

也许它可以帮助,至少对其他人。. .