您應該直接實現,而不是使用的便利性(和有些傳統)類PropertyValueFactory
單元格的值工廠。
假設你columnSize
是TableColumn<File, Number>
,你可以做
columnSize.setCellValueFactory(cellData ->
new SimpleLongProperty(cellData.getValue().length()));
如果您希望將數據列更優雅的格式,你還可以設置單元格工廠:
columnSize.setCellFactory(col -> new TableCell<File, Number>() {
@Override
protected void updateItem(Number length, boolean empty) {
super.updateItem(length, empty);
if (empty) {
setText(null);
} else {
setText(formatFileLength(length.longValue()));
}
}
});
// ...
private String formatFileLength(long length) {
final String[] unitNames = {"bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"};
int i ;
for (i = 0 ; length > 1024 && i < unitNames.length - 1 ; i++) {
length = length/1024 ;
}
return String.format("%,d %s", length, unitNames[i]);
}