第一次加载程序时,自动完成组合框的箭头键选择起作用。但是,在我使用箭头键下方的方法清除并重新加载其中的值后,选择组合框项目不起作用。 我使用ControlsFx 8中的TextFields.bindAutoCompletion方法来绑定组合框。
// load Auto Compleat nic,fname,lname,id values for search Combo Box
private void loadValuesToComboSearchBox() {
try {
// clear elements of Search Combo Box
comboSearch.getItems().clear();
// Disable Search Combo Box
comboSearch.setEditable(false);
// load Auto Compleat nic,fname,lname,id values for search Text Field
ResultSet getsearchElements = employeDao.getSearchemployeeByNicFnameLnmeId();
LinkedHashSet<String> addElements = new LinkedHashSet<>();
while (getsearchElements.next()) {
addElements.add(getsearchElements.getString("id"));
addElements.add(getsearchElements.getString("firstname"));
addElements.add(getsearchElements.getString("lastname"));
addElements.add(getsearchElements.getString("nic"));
}
// Add elements to Auto Compleat Text Field
TextFields.bindAutoCompletion(comboSearch.getEditor(), addElements);
// Enable Search Combo Box
comboSearch.setEditable(true);
} catch (Exception e) {
new Alert(Alert.AlertType.INFORMATION, e + "", ButtonType.OK).showAndWait();
e.printStackTrace();
}
}
答案 0 :(得分:0)
我认为,由于多个重叠的AutoCompletionBinding
导致出现此问题。
为了解决该问题,您应该保留对先前绑定的引用,并在重新加载时将其处置。另外,请注意,要清除JavaFX组合框中的选择,应执行更多步骤。
private AutoCompletionBinding<String> completion;
....
private void loadValuesToComboSearchBox() {
try {
// clear selection
comboSearch.getItems().clear();
comboSearch.valueProperty().set(null);
comboSearch.getEditor().clear();
comboSearch.setEditable(false);
ResultSet getSearchElements = employeDao.getSearchemployeeByNicFnameLnmeId();
Set<String> addElements = new LinkedHashSet<>();
while (getSearchElements.next()) {
addElements.add(getSearchElements.getString("id"));
addElements.add(getSearchElements.getString("firstname"));
addElements.add(getSearchElements.getString("lastname"));
addElements.add(getSearchElements.getString("nic"));
}
if (completion != null) {
completion.dispose();
}
completion = TextFields.bindAutoCompletion(comboSearch.getEditor(), addElements);
comboSearch.setEditable(true);
} catch (Exception e) {
new Alert(Alert.AlertType.INFORMATION, e + "", ButtonType.OK).showAndWait();
e.printStackTrace();
}
}