java 如何制作用于搜索的文本字段(使用类似谷歌搜索的提示)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6478577/
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-30 16:02:33  来源:igfitidea点击:

How to make a text field for searching(with tips like a google search)

javaswinguser-interfacejtextfield

提问by maximus

I want to make a window application for PC using java swing. I would like to make a text field there. While typing to that text field I need it to show tips under that text field. And user can select the needed text from the list. The same like the google searching does in the browser. So I need some two functions, first one is easy: filter a set of strings by already typed text. But how to show them in a list ?

我想使用 java swing 为 PC 制作一个窗口应用程序。我想在那里制作一个文本字段。在输入该文本字段时,我需要它在该文本字段下显示提示。用户可以从列表中选择所需的文本。就像谷歌搜索在浏览器中所做的一样。所以我需要一些两个函数,第一个很简单:通过已经输入的文本过滤一组字符串。但是如何在列表中显示它们?

EDIT1: I need the list to be shown if there is something to show after filtration, and an ability to select by using keys up and down. The same as google search but on PC application.

EDIT1:如果过滤后要显示某些内容,我需要显示列表,并且可以使用上下键进行选择。与谷歌搜索相同,但在 PC 应用程序上。

采纳答案by trashgod

JComboBoxis a common choicefor this. AutoCompleteJComboBoxeris one I've tried.

JComboBox这是一个常见的选择AutoCompleteJComboBoxer是我试过的。

回答by Heisenbug

I suggest you to add a DocumentChangeListener to your JTextField to monitor the inserted/deleted/update chars:

我建议您将 DocumentChangeListener 添加到 JTextField 以监视插入/删除/更新字符:

JTextField textField = new JTextField;
textField.addDocumentListener (new DocumentListener() {
  public void changedUpdate(DocumentEvent e) {
  }
  public void removeUpdate(DocumentEvent e) {
  }
  public void insertUpdate(DocumentEvent e) {
  }
}
);

Have a look of this tutorial.

看看这个教程

So I need some two functions, first one is easy: filter a set of strings by already typed text.

所以我需要一些两个函数,第一个很简单:通过已经输入的文本过滤一组字符串。

Now that you have the typed text, filter your set of strings. Be careful in choosing an efficient way to store and retrieve strings from your data structure. With a lot of string to sort this may not be trivial. (I think it will be hard to this with an ArrayList if you set of strings if quite big.)

既然您已经输入了文本,请过滤您的字符串集。在选择一种有效的方式来存储和检索数据结构中的字符串时要小心。有很多字符串要排序,这可能不是微不足道的。(我认为如果你设置的字符串非常大,那么使用 ArrayList 会很困难。)

But how to show them in a list ?

但是如何在列表中显示它们?

I think you could use a JLabel. Alternatively you can try with a JComboBox implementing your own ComboBoxModel. I don't know if it is possible to keep always open the combo box.

我认为您可以使用 JLabel。或者,您可以尝试使用 JComboBox 实现您自己的ComboBoxModel。我不知道是否可以始终打开组合框。

回答by Abhishek Choudhary

Well .... in the JTextField , add a listener for keypress and then as soon as any key is getting pressed , use a set to sort all the related elements. An option is having the String to toChar for getting each character and then post the records in the JList.

嗯.... 在 JTextField 中,为按键添加一个监听器,然后只要按下任何键,就使用集合对所有相关元素进行排序。一个选项是让字符串到 toChar 来获取每个字符,然后在 JList 中发布记录。

Secondly , you better keep your records in an XML then tracking will be easier rest any kind of sorting algorithm logic can be easily applied.

其次,您最好将记录保存在 XML 中,这样跟踪就会更容易,其他任何类型的排序算法逻辑都可以轻松应用。