IndexOutOfBoundsException while clearing SearchableComboBox
See original GitHub issueClearing SearchableComboBox items almost always leads to IndexOutOfBoundsException.
Exception in thread "JavaFX Application Thread" java.util.NoSuchElementException: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.base/java.util.AbstractList$Itr.next(AbstractList.java:377)
at java.base/java.util.AbstractList.indexOf(AbstractList.java:188)
at javafx.controls/javafx.scene.control.ComboBox.lambda$new$1(ComboBox.java:245)
at javafx.base/com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:360)
at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.base/javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:106)
at javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:113)
at javafx.base/javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147)
at javafx.base/javafx.beans.property.ObjectProperty.setValue(ObjectProperty.java:72)
at javafx.base/com.sun.javafx.binding.BidirectionalBinding$TypedGenericBidirectionalBinding.changed(BidirectionalBinding.java:585)
at javafx.base/com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:360)
at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.base/javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:106)
at javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:113)
at javafx.base/javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147)
How to reproduce
Run this code and try to clear SearchableComboBox several times. In the 90% it will raise exception. Clearing normal combobox does not produce any error.
public class Launcher extends Application {
VBox vbox;
Button populateBtn;
SearchableComboBox<String> searchableComboBox;
ObservableList<String> searchableBase = FXCollections.observableArrayList();
Button clearSearchableBtn;
ComboBox<String> filteredComboBox;
ObservableList<String> filteredBase = FXCollections.observableArrayList();
Button clearFilteredBtn;
@Override
public void start(Stage primaryStage) throws Exception {
vbox = new VBox();
searchableComboBox = new SearchableComboBox<>(searchableBase);
searchableBase.setAll(getItems());
searchableComboBox.getSelectionModel().selectLast();
searchableComboBox.setPrefWidth(200);
clearSearchableBtn = new Button("Clear searchable");
clearSearchableBtn.setOnAction(event -> {
Platform.runLater(() -> {
// don't really matter
// searchableComboBox.getSelectionModel().clearSelection();
searchableBase.clear();
});
});
filteredComboBox = new ComboBox<>(new FilteredList<>(filteredBase, val -> true));
filteredBase.setAll(getItems());
filteredComboBox.getSelectionModel().selectLast();
filteredComboBox.setPrefWidth(200);
clearFilteredBtn = new Button("Clear filtered");
clearFilteredBtn.setOnAction(event -> {
Platform.runLater(() -> {
filteredBase.clear();
});
});
populateBtn = new Button("Populate");
populateBtn.setOnAction(event -> {
searchableBase.setAll(getItems());
searchableComboBox.getSelectionModel().selectLast();
filteredBase.setAll(getItems());
filteredComboBox.getSelectionModel().selectLast();
});
vbox.getChildren().addAll(
searchableComboBox, clearSearchableBtn,
filteredComboBox, clearFilteredBtn,
populateBtn
);
vbox.setSpacing(10);
Scene scene = new Scene(vbox, 400, 400);
primaryStage.setTitle("Demo");
primaryStage.setScene(scene);
primaryStage.setOnCloseRequest(t -> Platform.exit());
primaryStage.show();
}
private static List<String> getItems() {
return List.of("test1", "test2", "foo");
}
}
It may be a similar issue except Platform.runLater()
doesn’t make any difference. The only thing that helps is setting a new observable list each time you want to update items, which I’d want to avoid, because it resets bindings.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:7 (1 by maintainers)
Top Results From Across the Web
java - JavaFX 11 Editable ComboBox throws ... - Stack Overflow
... in an application, but this throws IndexOutOfBoundsException when the edited item is committed. I do not understand why this should be.
Read more >IndexOutOfBoundsException in CheckComboBox when ...
IndexOutOfBoundsException in CheckComboBox when clearing selection #347 ... While I can build the snapshot 8.0.7 with gradle, I can't build ...
Read more >IndexOutOfBoundsException when using "in" function in ...
I have trouble with the in filter function in SLD submitted to GeoServer. The root cause seems to be within the geotools. Currently...
Read more >Error "java.lang.IndexOutOfBoundsException" when deleting ...
Learn how to resolve error "java.lang.IndexOutOfBoundsException" when deleting message content folder in EngageOne Server. This has been identified as a ...
Read more >[JDK-8133228] [ComboBox] IndexOutOfBoundsException ...
[ComboBox] IndexOutOfBoundsException when onAction handler changes the selection. · Details · Description · Attachments · Attachments · Activity.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
What comes to my mind is “Free as in beer”… ControlsFX is a free open source library given to you at no cost and you should not have any expectations.
Having said that, I believe everyone is welcome to contribute and to improve
As a workaround, adding the new list in an
observableArrayList
and setting it as below fixed the issue for me.