Exception when using CheckListView of Controls FX

332 Views Asked by At

I have the error :

Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: Index: 0

I thought it was because I have to use Platform.runLater(), but it seems the errors does not come from this.

Here's the function that I try to correct with Platform.runLater() :

public void setListAirportForFilter(ListAirport listAirport){
    this.myListAirport = listAirport;

    departureCheckListView.setItems(myListAirport.getObservableDepartureAirtport());
    arrivalCheckListView.setItems(myListAirport.getObservableArrivalAirport());

    departureCheckListView.getCheckModel().getCheckedItems().addListener(new ListChangeListener<String>() {
        @Override
        public void onChanged(ListChangeListener.Change<? extends String> c) {
            c.next();
            if(c.wasAdded()) {  
                observableForbiddenDeparture.add(c.getAddedSubList().get(0));
                System.out.println("Item Checked : " + c.getAddedSubList().get(0));
            } 
            else if (c.wasRemoved()) {
                observableForbiddenDeparture.remove(c.getAddedSubList().get(0));
                System.out.println("Item Unchecked : " + c.getRemoved().get(0));
            }
        }
     });

}

The error triggered in the second case, with the removed.

1

There are 1 best solutions below

0
On BEST ANSWER

If an element is removed and it calls the ListChangeListener, then your change will have the list of items removed and not the list of items added.

In your second case, you are checking for c.wasRemoved(), which means if it is true, elements were removed from the ObservableList. All your removed values are stored inside getRemoved() sub-list and if you try to fetch getAddedSubList(), you will get an empty list.

You need to use

observableForbiddenDeparture.remove(c.getRemoved().get(0));

instead of

observableForbiddenDeparture.remove(c.getAddedSubList().get(0));