Search code examples
javajavafxjavafx-8observablelist

Finding the Index of an Item that was already Deleted


I have an original ObservableList<PlanItem> src.

I create a SortedList from this src, and call it sortedList.

From the sortedList, I want to create an ObservableList<XYChart.Data>, called dataSet. sortedList and dataSet will always be of the same size. sortedList has PlanItems, dataSet has XYChart.Data nodes representative of those PlanItems.

If a PlanItem is deleted from the src, the sortedList will also delete it. At the same time, I want to delete its respective XYChart.Data node from the dataSet.

The problem is, I don't know what was just deleted from the sortedList. I can't find a way to determine the index that was just deleted. Without knowing this index, I have no way of deleting the respective XYChart.Data node from the dataSet.


Solution

  • This is a really nice use case for the EasyBind framework. You can just create a mapping from sortedList to dataSet and it will handle all the additions and deletions for you.

    The code would look something like

    ObservableList<XYChart.Data> dataSet = EasyBind.map(sortedList, this::makeChartData) ;
    
    // ...
    
    private XYChart.Data makeChartData(PlanItem planItem) {
        // Just return a XYChart.Data corresponding to planItem
    }