我正在开发一个JavaFX项目。我正在使用ControlFx的Autcomplete TextField。每次我在数据库表中添加新行时,它应该更新自动完成,我这样做但我的问题显示double Context-Menu ,我们可以说是双重自动填充,因为我调用了在表中添加新元素时创建自动完成的方法。
当我点击标签editBill
时,我会调用此方法:
public void showEditBill() {
if (!BillPane.getTabs().contains(EditBillTab)) {
BillPane.getTabs().add(EditBillTab);
}
SingleSelectionModel<Tab> selectionModel = BillPane.getSelectionModel();
selectionModel.select(EditBillTab);
/*it should remove the old autocomplete from textfield*/
pushBills(); //Call for cheking new items
}
pushBills方法():
public void pushBills() {
ArrayList list = new ArrayList<>();
bills = new BillHeaderDao().FindAll();
for (int i = 0; i < bills.size(); i++) {
list.add(bills.get(i).getIdClient());
}
//How can i remove the old bind before bind again
autoCompletionBinding = TextFields.bindAutoCompletion(SearchBill, SuggestionProvider.create(list));
}
我如何删除旧的自动填充并绑定新的自动填充功能?
答案 0 :(得分:3)
在任何情况下,如果你需要保留AutoCompletionTextFieldBinding对象的实例,从而避免使用:
autoCompleteBinding = TextFields.bindingAutoCompletion(TextField,List);
,这将改变实例,我们可以更深入一点并使用它:
// let's suppose initially we have this possible values:
Set<String> autoCompletions = new HashSet<>(Arrays.asList("A", "B", "C"));
SuggestionProvider<String> provider = SuggestionProvider.create(autoCompletions);
new AutoCompletionTextFieldBinding<>(textField, provider);
// and after some times, possible autoCompletions values has changed and now we have:
Set<String> filteredAutoCompletions = new HashSet<>(Arrays.asList("A", "B"));
provider.clearSuggestions();
provider.addPossibleSuggestions(filteredAutoCompletions);
所以,通过SuggestionProvider,我们已经更新了#34;自动完成值。 为避免建议菜单加倍,请不要再次使用(第二次):
TextFields.bindAutoCompletion(..)
答案 1 :(得分:1)
为了提供对自动完成建议列表的更新,请保留对RecommendationionProvider的引用并更新建议提供者:
TextField textField = new TextField();
SuggestionProvider suggestionProvider = SuggestionProvider.create(new ArrayList());
new AutoCompletionTextFieldBinding<>(textField, suggestionProvider);
要更新建议列表时:
List<String> newSuggestions = new ArrayList();
//(add entries to list)
suggestionProvider.clearSuggestions();
suggestionProvider.addPossibleSuggestions(newSuggestions);
答案 2 :(得分:0)
这样就可以了:
而不是:TextFields.bindAutoCompletion(textField, list);
试试这个:
List<String> strings = new ArrayList<>();
然后在textField与列表之间创建绑定:
new AutoCompletionTextFieldBinding<>(textField, SuggestionProvider.create(strings));
因此,列表中的任何更改(包括删除)都将反映在textField的autoCompletion中; 当用户在textField中输入一些文本时,您将对弹出窗口中的建议进行动态过滤;
答案 3 :(得分:0)
前段时间我遇到了同样的问题,我试图像@MaxKing提到的那样,但它没有用。我设法给它一个解决方案,即使我不认为这是正确的方式。
//处理旧绑定并重新创建新绑定
autoCompleteBinding.dispose();
autoCompleteBinding = TextFields.bindingAutoCompletion(TextField,List);
试试这个:
public void pushBills() {
ArrayList list = new ArrayList<>();
bills = new BillHeaderDao().FindAll();
for (int i = 0; i < bills.size(); i++) {
list.add(bills.get(i).getIdClient());
}
autoCompletionBinding .dispose();
autoCompletionBinding = TextFields.bindAutoCompletion(SearchBill, SuggestionProvider.create(list));
}