当我想随机播放FilteredList时,我得到了java.lang.UnsupportedOperationException

如何处理呢?

码:

FilteredList<Card> filteredData =
    new FilteredList(ob, filterByOption(option.get("selectedCard"), option.get("chapter")));

if (option.get("cardOrder") == "shuffle") {
    filterCards=filteredData;
    FXCollections.shuffle(filterCards);
}

最佳答案

documentation所写:

包装一个ObservableList 并使用提供的内容过滤其内容
谓词。 传播ObservableList中的所有更改
立即到FilteredList。


因此,您可以改组基础源ObservableList:

FXCollections.shuffle(ob);

示例:
ObservableList<String> obsList =
    FXCollections.observableArrayList("Amanda", "Bill", "Adam", "Albus", "Cicero");
FilteredList<String> fList = new FilteredList<>(obsList, s -> s.startsWith("A"));

System.out.println(fList);
FXCollections.shuffle(obsList);
System.out.println(fList);

输出:
[Amanda, Adam, Albus]
[Adam, Albus, Amanda]

10-07 19:20