ArrayList<String> aniNames = new ArrayList<>();我做了与数据库的连接,并在上面的ArrayList中存储了7K动漫名称。
JFXAutoCompletePopup<String> autoCompPop = new JFXAutoCompletePopup<>();
for (int i=0; i<aniNames.size(); i++){
autoCompPop.getSuggestions().addAll(aniNames.get(i));
}
autoCompPop.setSelectionHandler(event ->{
autoTF.setText(event.getObject());
});
autoTF.textProperty().addListener(observable -> {
autoCompPop.filter(string -> string.toLowerCase().contains(autoTF.getText().toLowerCase()));
if (autoCompPop.getFilteredSuggestions().isEmpty() || autoTF.getText().isEmpty()) {
autoCompPop.hide();
} else {
autoCompPop.show(autoTF);
}
});所以我写了这个程序,当我输入一些东西的时候,它会弹出自动补全。
当我输入一个字母,让我们说"a“或任何其他字母时,它显示几乎所有的动漫名称中都有"a”,这意味着几乎所有的7k动漫。
我想限制它显示的结果数量,如果超过一定的数字,例如,如果超过20个动漫在他们的名字中有"a“,那么我希望它只在弹出窗口中显示前20个动漫。
发布于 2018-10-17 04:42:09
更改:
autoCompPop.filter(string -> string.toLowerCase().contains(autoTF.getText().toLowerCase()));至:
autoCompPop.filter(string -> string.toLowerCase().startsWith(autoTF.getText().toLowerCase()));可能会帮助你,不是通过强制限制结果,而是通过告诉弹出窗口搜索以输入的文本开头的名称,而不是输入的文本的名称,从而减少结果的数量。
JFXAutocompletePopup似乎没有限制搜索结果的方法,因为setCellLimit()只限制可见单元格的数量(本质上是缩短弹出窗口的高度,而不是实际的项目数)。JFXAutocompletePopup source code
https://stackoverflow.com/questions/51809535
复制相似问题