【发布时间】:2017-02-04 09:11:49
【问题描述】:
我正在运行 Java 8u102。我有一个模式窗口,其中包含一个Combobox,其项目是从字符串列表创建的FilteredList。 ComboBox 是可编辑的,以便用户可以输入文本(自动转换为大写)。然后过滤ComboBox 弹出窗口中的项目,以便仅保留以输入文本开头的项目。这很好用。
问题是,当您单击过滤弹出窗口中的项目时,所选项目将正确显示在组合框编辑器中并且弹出窗口将关闭,但会抛出IndexOutOfBoundsException,可能从创建的代码开始在线窗口 - stage.showAndWait()。下面是运行ComboBox 的代码。
对解决方法有什么建议吗?我计划向组合框添加更多功能,但我想先处理这个问题。谢谢。
FilteredList<String> filteredList =
new FilteredList(FXCollections.observableArrayList(myStringList), p -> true);
cb.setItems(filteredList);
cb.setEditable(true);
// convert text entry to uppercase
UnaryOperator<TextFormatter.Change> filter = change -> {
change.setText(change.getText().toUpperCase());
return change;
};
TextFormatter<String> textFormatter = new TextFormatter(filter);
cb.getEditor().setTextFormatter(textFormatter);
cb.getEditor().textProperty().addListener((ov, oldValue, newValue) -> {
filteredList.setPredicate(item -> {
if (item.startsWith(newValue)) {
return true; // item starts with newValue
} else {
return newValue.isEmpty(); // show full list if true; otherwise no match
}
});
});
【问题讨论】:
标签: java javafx combobox javafx-8 indexoutofboundsexception