I am running Java 8u102. I have a modal window that contains a Combobox
whose items are a FilteredList
created from a list of Strings. The ComboBox
is editable so that the user can enter text (automatically converted to uppercase). The items in the ComboBox
popup are then filtered such that only those items that start with the entered text remain. This works great.
The problem is that when you click an item in the filtered popup, the selected item will be properly displayed in the combobox editor and the popup will close, but an IndexOutOfBoundsException
is thrown, probably starting in the code that created the window at the line - stage.showAndWait()
. Below is the code running the ComboBox
.
Any suggestions for a work-around? I plan to add more functionality to the combobox, but I'd like to deal with this issue first. Thanks.
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
}
});
});