0

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.

ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
Evans Belloeil
  • 2,413
  • 7
  • 43
  • 76
  • How you concluded that you should to use runLater()? Namely why did you need it? – Uluk Biy Jun 19 '15 at 08:27
  • Because this observableList interact with the GUI – Evans Belloeil Jun 19 '15 at 08:32
  • I think you don't need it here, because if some user action triggered selection, it is already in ui thread, but if you are changing CheckedItems or `myListAirport.getObservableArrivalAirport` or some other list you should not think that you are the only listener of event. If you change `CheckedItems` lots of things will trigger. Not only your listener, but view will try to ivalidate and redraw itself. So you need to use Platform.runLater when you are actually changing Observable value in another thread. – varren Jun 19 '15 at 08:35
  • From where is the `IllegalStateException : Invalid Change state: next() must be called before inspecting the Change.` thrown? – ItachiUchiha Jun 19 '15 at 08:37
  • @ItachiUchiha from this line : observableForbiddenArrival.add(c.getAddedSubList().get(0)); – Evans Belloeil Jun 19 '15 at 08:37
  • @EvansBelloeil You do not need `Platform.runLater()` because JavaFX properties and their change listeners can executable on non-JavaFX threads. As @varren said since user action is triggered on `CheckListView`, which means you already are on `JavaFX application thread` and you do not need it. Moreover, your initial exception was `IndexOutOfBoundsException`, which means you are trying to access an index value in an array which doesn't exist. – ItachiUchiha Jun 19 '15 at 08:52
  • Ok i'm sorry, I always associated this exception with Platform.runLater, and I always find this answer on the web. I will edit my post so. – Evans Belloeil Jun 19 '15 at 08:56

1 Answers1

1

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));
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176