Search code examples
tornadofx

Can't remove ListView item in tornadoFX


There is two listview, I can drag an item into another, but the dragged item won't be deleted from the source list.

I know the code is not the right way to handle data, however I think it should work, but it just don't.

     ...
    setOnDragDone {event ->
       removeSelected(event.gestureSource)
    }
    ...
    fun  removeSelected(gestureTarget: Any?) {
        var listview = gestureTarget as ListView<String>
        val modelItems = FXCollections.observableList(listview.selectionModel.selectedItems)
        if (modelItems != null) {
            listview.selectionModel.clearSelection()
            listview.items.removeAll(modelItems)
        }
    }

Thanks.


Solution

  • You're overthinking this and introducing some traps for yourself along the way :)

    You create the modelItems list as an observable list, backed by the ListView's selectedItems property. So changes to the selected items in the ListView will be reflected in your modelItems property.

    When you call selectionModel.clearSelection(), your modelItems list is also empty, so you're calling removeAll(anEmptyList).

    Just do listview.items.removeAll(listview.selectionModel.selectedItems). The ListView will update the selected items list when the items are removed from the backing list.