Ramses
Ramses

Reputation: 672

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

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.

Upvotes: 5

Views: 18489

Answers (4)

SedJ601
SedJ601

Reputation: 13859

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

Upvotes: 8

Anupam Dewan
Anupam Dewan

Reputation: 51

If you are trying to clear all the items from your tableView and want just an empty tableView. You can use this:

myTableView.getItems().clear();

This basically gets all the items from your table view which is nothing but just the observable list now it performs clear operations to remove all the items in tableView.

Upvotes: 2

aw-think
aw-think

Reputation: 4803

As James_D already mentioned, you haven't cleared exactly what's the point.

If you want to delete selected items from a table, you need to delete them from the table item list itself and not from the selection model.

A possible solution looks like this:

TableView<String> table = new TableView<>();
ObservableList<String> tableItems = table.getItems();

// needs multirowselection is set to true
ObservableList<String> readOnlyItems = table.getSelectionModel().getSelectedItems(); 

// removes all selected elements for the table
readOnlyItems.stream().forEach((item) -> {
    tableItems.remove(item);
});

// clear the selection
table.getSelectionModel().clearSelection();

Update

This method get's an TableView, calls it's selection model to get all selected items. And then you add the data to a new TableView. And there is the problem! It's an unmodifiable read only list that you attached to your new table. First make it modifiable, like in the code below:

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

The next problem is in your call to this method. You call it with a TableView<CsvData> and with a TableView<String> as your method needs. If CsvData is a subtype of String, than you have to change your method signature to TableView<? extends String>

Upvotes: 3

James_D
James_D

Reputation: 209418

Assuming you mean

table.getSelectionModel().getSelectedItems()

(since the selection model has no getItems() method), according to the Javadocs, this returns a read-only list. Thus attempting to modify the list will throw an UnsupportedOperationException.

To clear the selection, do

table.getSelectionModel().clearSelection();

(And similarly, if you want to manipulate the selection in any other way, you use methods on the selection model, rather than on the list.)

Upvotes: 2

Related Questions