Can't clear all items (elements) in an ObservableList

Ramses picture Ramses · Jun 29, 2015 · Viewed 14.4k times · Source

I've a couple of copied elements in an observablelist which I use for copy/paste operations in a TableView. The name of the table is cpTable (copy and paste Table) for storing copied elements and to paste elements that are stored in the table. After each paste operation I want to clear the contents of cpTable before I copy other selected items with Ctrl+C. But I always get the error:

JavaFX Application Thread" java.lang.UnsupportedOperationException: Not supported.
at com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList.remove(ReadOnlyUnbackedObservableList.java:246)

Here is my pseudocode:

if (cpTable !=null) {
    //first, get all copied items for removing all elements 
    ObservableList<String> copiedItems = cpTable.getItems();                                 
    int size = copiedItems.size();

    // remove all elements 
    for(int i=0;i<size;i++) {
        copiedItems.remove(i);
    }
    cpTable.setItems(copiedItems); //clear cpTable by setting an empty list
}

This is a method that copies the contents of selected items and puts it in a cpTable

public TableView<String> copySelectionToClipboard(TableView<String> table) {            
    ObservableList<String> data =     table.getSelectionModel().getSelectedItems();             
    TableView<String> tmp = new TableView<>();
    tmp.setItems(data);
    return tmp;

}

When Ctrl+C is pressed the following line puts all copied items in a cpTable:

cpTable = copySelectionToClipboard( (TableView<String>) keyEvent.getSource());

As mentioned I want to clear all cpTable contents immediately after pasting the items in a table.

Answer

Sedrick picture Sedrick · Apr 8, 2016

Just clear your Observable List. It looks like you should use copiedItems.clear(); That should clear your table.