user3235815
user3235815

Reputation: 89

How to shuffle a FilteredList in java?

When I want to shuffle an FilteredList I get a java.lang.UnsupportedOperationException.

How to handle this?

Code:

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

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

Upvotes: 4

Views: 269

Answers (1)

DVarga
DVarga

Reputation: 21809

As written in the documentation:

Wraps an ObservableList and filters it's content using the provided Predicate. All changes in the ObservableList are propagated immediately to the FilteredList.

Therefore, you can shuffle the underlying source ObservableList instead:

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

Output:

[Amanda, Adam, Albus]
[Adam, Albus, Amanda]

Upvotes: 5

Related Questions