Issue
I'm trying to sort a TableView table
by a certain column. When I display the table, it's shown but from what I'm finding on here and other sites it's just confusing me more and more.
here is the tableview code
public TableView<Animal> animalLostLocation(ArrayList<Animal> list)
{
TableColumn<Animal, String> atypeColumn = new TableColumn<>("Animal Type");
atypeColumn.setMinWidth(200);
atypeColumn.setSortable(false);
atypeColumn.setCellValueFactory(new PropertyValueFactory<>("aType"));
TableColumn<Animal, String> descriptionColumn = new TableColumn<>("Description");
descriptionColumn.setMinWidth(200);
descriptionColumn.setSortable(false);
descriptionColumn.setCellValueFactory(new PropertyValueFactory<>("description"));
TableColumn<Animal, String> breedColumn = new TableColumn<>("Breed");
breedColumn.setMinWidth(80);
breedColumn.setSortable(false);
breedColumn.setCellValueFactory(new PropertyValueFactory<>("breed"));
TableColumn<Animal, String> nameColumn = new TableColumn<>("Name");
nameColumn.setMinWidth(200);
nameColumn.setSortable(false);
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
TableColumn<Animal, Catergory> catColumn = new TableColumn<>("Category");
catColumn.setMinWidth(200);
breedColumn.setSortable(false);
catColumn.setCellValueFactory(new PropertyValueFactory<>("category"));
TableColumn<Animal, Integer > ageColumn = new TableColumn<>("Age");
ageColumn.setMinWidth(50);
ageColumn.setSortable(false);
ageColumn.setCellValueFactory(new PropertyValueFactory<>("age"));
TableColumn<Animal, Integer > idColumn = new TableColumn<>("ID");
idColumn.setMinWidth(50);
idColumn.setCellValueFactory(new PropertyValueFactory<>("ID"));
TableColumn<Animal, Boolean > genColumn = new TableColumn<>("Male");
genColumn.setMinWidth(50);
genColumn.setSortable(false);
genColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
table = new TableView<Animal>();
table.setItems(getAnimal(list));
table.getColumns().addAll(genColumn, idColumn, nameColumn, atypeColumn, ageColumn, breedColumn, descriptionColumn, catColumn);
return table;
}
I want to sort the table by gender initially.
As far as I know I have to create a SortedList
/FilteredList
but the tutorial I found online is just confusing me even more.
Do I have to use SortedList
/FilteredList
or are there better alternatives?
Here's one of the links I found http://code.makery.ch/blog/javafx-8-tableview-sorting-filtering/
Can anyone dumb it down for me please?
Solution
There is no need to create a sorted list yourself. Just add the column to sort by to the sortOrder
list of your TableView
:
table.getSortOrder().add(genColumn);
Depending on the types displayed in the column(s), you may also want to set the comparator to use yourself (see TableColumnBase.comparatorProperty
)
This approach has the benefit of using the sorting provided by user interaction (clicking on the column headers) instead of sorting the backing list (or using a "sorted view" of a list, which is what SortedList
does).
Answered By - fabian
Answer Checked By - David Goodson (JavaFixing Volunteer)