按某些列Javafx对TableView进行排序 [英] Sort TableView by certain column Javafx
问题描述
我正在尝试按特定列对 TableView表
进行排序。当我显示表格时,它显示但是从我在这里和其他网站上发现的内容,它让我越来越困惑。
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.
这里是tableview代码
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.
据我所知,我必须创建一个 SortedList
/ FilteredList
但是我在网上找到的教程让我更加困惑。
As far as I know I have to create a SortedList
/FilteredList
but the tutorial I found online is just confusing me even more.
我是否必须使用 SortedList
/ FilteredList
或有更好的选择吗?
Do I have to use SortedList
/FilteredList
or are there better alternatives?
这是我找到的一个链接 http://code.makery.ch/blog/javafx-8-tableview-sorting-filtering/
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?
推荐答案
无需自己创建排序列表。只需添加列以按 sortOrder
TableView
的列表:
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);
根据列中显示的类型,您可能还需要将比较器设置为自己动手(参见 TableColumnBase.comparatorProperty
)
Depending on the types displayed in the column(s), you may also want to set the comparator to use yourself (see TableColumnBase.comparatorProperty
)
这种方法的好处是可以使用用户交互提供的排序(点击列标题)而不是排序后备列表(或使用列表的排序视图,这是 SortedList
确实)。
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).
这篇关于按某些列Javafx对TableView进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!