...哪里T
是 TableView
的泛型类型.
到目前为止,我正在实现一个包含三列的文件 ListView 。每个类型java.nio.file.Path
。对于名称栏,我写了 Comparator<Path>
它按目录优先且不区分大小写对文件进行排序。另外两个比较器按上次修改时间和文件大小排序。为此,他们比较 long
字段。
但是comparatorProperty
列基于 Comparator<String>
。我认为,根据显示的文本进行排序...
所以我必须找到一种方法,将标题点击排序功能与 TableView
的类型结合使用。 ?
最佳答案
在创建 TableColumn
时,您使用了错误的类型参数。如果您创建 TableColumn<Path, Path>
,您可以指定Comparator<Path>
对于该专栏。同样TableColumn<Path, FileTime>
和TableColumn<Path, Long>
使用Comparator<FileTime>
和Comparator<Long>
.
来自文档:
Class TableColumnBase<S,T>
Type Parameters:
S - The type of the UI control (e.g. the type of the 'row').
T - The type of the content in all cells in this table column.
关于JavaFx-表格 View : How to use custom Comparator<T> for sorting?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32085250/