我正在尝试制作一个 ComboBox,它具有从其项目中搜索匹配项的功能。
这是我所做的代码示例,
ObservableList<String> ab = FXCollections.observableArrayList("z", "asxdf", "abasdf", "bcasdf", "b", "bc", "bcd", "c");
final ComboBox box = new ComboBox(ab);
box.setEditable(true);
box.setOnKeyReleased(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
box.show();
for (String item : items) {
if (item.startsWith(box.getEditor().getText())) {
box.getSelectionModel().select(item); //which selects the item.
break;
}
}
}
});
现在的问题是box.getSelectionModel().select(item);
选择在 ComboBox 中键入的特定项目,但我不想选择该项目,我只想像鼠标悬停时那样悬停(聚焦)该项目。
谁能告诉我要替换的代码box.getSelectionModel().select(item);
并帮助我解决这个问题。