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

4

5

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.

Entire answered 29/6, 2015 at 15:17 Comment(0)
P
8

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

Patterson answered 8/4, 2016 at 15:56 Comment(0)
M
3

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>

Myxomatosis answered 29/6, 2015 at 17:55 Comment(3)
ok, I edited my question. Please have a look at my pseudocode, too.Entire
@Entire How do you create the table items? Or the list of table items? Can you show your code. Maybe you get a Collection from FXCollections that is not initialized by a backed Collection or is read only.Myxomatosis
my code is too large and too complex to put it here. Theefore, I've added the most important methods, copySelectionToClipboard and a line for cpTable declarationEntire
N
2

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.)

Negotiable answered 29/6, 2015 at 15:24 Comment(1)
ok, I edited my question. Please have a look at my pseudocode again. I don't want to clear the table selection. Instead, I want to clear all items in a list.Entire
H
2

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.

Hosey answered 22/11, 2017 at 17:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.