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?
Set sort type for column that you want to sort, add that column to sort order, then call sort() method where ever you want to sort table.
I would add a multi-select button like android. Click the button the subsequent selections get added (or maybe removed after another click) to a list of selections.
We can filter TableView content in two main ways – manually, or by using the FilteredList class JavaFX provides. In either case, we can update our search criteria by placing a ChangeListener on the search box TextField. This way, each time the user changes their search, the TableView is updated automatically.
passing in the TableColumn (s) by which you want the data sorted. To make the sort persist even as the items in the list change, create a SortedList and pass it to the table's setItems(...) method. (To change the items, you will still manipulate the underlying list.)
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With