javajavafxobservablelist

JavaFX8: exception when adding items to ObservableList: ConcurrentModificationException


I have an a ChoiceBox that lists months values, when the user choose a value, it execute this lambda expression:

private TableView<IncomeFX> tableIncome;
private ChoiceBox<Month> choiceBoxIncomeMonths;

private ChangeListener<Month> setChoiceBoxIncomeMonthsBehaviour(){
    ChangeListener<Month> months = (ObservableValue<? extends Month> observable, Month oldValue, Month newValue) -> {
            incomesData.clear();
            Year selectedYear = choiceBoxIncomeYears.getSelectionModel().getSelectedItem();
            ObservableList<IncomeFX> temp = incomeManager.getIncomesOf(selectedYear, newValue);
            incomesData.addAll(temp);

    };
    return months;
}

and how i add the listener:

choiceBoxIncomeMonths.getSelectionModel().selectedItemProperty().addListener(setChoiceBoxIncomeMonthsBehaviour());

when I click on the choicebox, i get:

Exception in thread "JavaFX Application Thread" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:386)
at java.util.AbstractList$Itr.next(AbstractList.java:355)
at java.util.AbstractCollection.addAll(AbstractCollection.java:343)
at javafx.collections.ModifiableObservableListBase.addAll(ModifiableObservableListBase.java:99)
at lite.money.ui.MainUI.lambda$1(MainUI.java:160)
at lite.money.ui.MainUI$$Lambda$120/1680764266.changed(Unknown Source)

it indicate that the problem is in the line where I call: addAll(temp) how can i solve this ??? thanks


Solution

  • here is how i solve it, it's a bad code i know, but i don't know any other solution, i have to clear it twice or the items will be added like if i didn't clear it, if you do have an other solution i will be happy:

    private ChangeListener<Month> setChoiceBoxIncomeMonthsBehaviour(){
        ChangeListener<Month> months = (ObservableValue<? extends Month> observable, Month oldValue, Month newValue) -> {
            if (!lastMonthValuesFired) {
                incomesData.clear();
                Year selectedYear = choiceBoxIncomeYears.getSelectionModel().getSelectedItem();
                ObservableList<IncomeFX> temp = incomeManager.getIncomesOf(selectedYear, newValue);
                ObservableList<IncomeFX> temp2 = FXCollections.observableList(new ArrayList<IncomeFX>());
    
                for (IncomeFX t : temp) {
                    temp2.add(t);
                }
                incomesData.clear();
                incomesData.addAll(temp2);
            }
        };
        return months;
    }