Banjer_HD
Banjer_HD

Reputation: 300

JavaFX: Can't set items of TableView to FilteredList of its own items

I am trying to set the items of a TableView to a FilteredList of its own items but I get an error doing so.

public class FilteredTableView<T> {

    private TableView<?> tableView;
    FilteredList<?> filteredData;

    public <T> FilteredTableView(TableView<T> tableView) {
        this.tableView = tableView;
        this.filteredData = new FilteredList<>(tableView.getItems(), s -> true);
        this.tableView.setItems(this.filteredData); // The method setItems(ObservableList<capture#4-of ?>) in the type TableView<capture#4-of ?> is not applicable for the arguments (FilteredList<capture#5-of ?>)
    }

}

Is there a way to solve this without having to make a new FilteredTableViewObjectX class for every different Object I want to put in a different TableView? Thanks for helping!

Upvotes: 1

Views: 221

Answers (1)

James_D
James_D

Reputation: 209553

Since your class is already parameterized with the type T, your TableView and FilteredList variables can refer to the same type.

Note that the method should not be parameterized, because then T in the method will refer to a different type variable than T in the class (leading to really interesting compile errors such as "Cannot convert from TableView<T> to TableView<T>").

public class FilteredTableView<T> {

    private TableView<T> tableView;
    FilteredList<T> filteredData;

    public FilteredTableView(TableView<T> tableView) {
        this.tableView = tableView;
        this.filteredData = new FilteredList<>(tableView.getItems(), s -> true);
        this.tableView.setItems(this.filteredData); 
    }

}

Upvotes: 4

Related Questions