How to shuffle a FilteredList in java?

When I want to shuffle FilteredList, I get java.lang.UnsupportedOperationException.

How to handle this?

the code:

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

if (option.get("cardOrder") == "shuffle") {
    filterCards=filteredData;
    FXCollections.shuffle(filterCards);
}
+4
source share
1 answer

As written in the documentation :

Wraps around the ObservableList and filters its contents using the provided Predicate. All changes to the ObservableList are propagated immediately to the FilteredList.

Therefore, you can shuffle the source code ObservableList:

FXCollections.shuffle(ob);

Example:

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

Conclusion:

[Amanda, Adam, Albus]
[Adam, Albus, Amanda]
+5
source

Source: https://habr.com/ru/post/1678230/


All Articles