我想在TableView中打印带有to小数的浮点数。但是格式化浮点数会破坏列的排序。
TableColumn<Model, String> profit = new TableColumn<Model, String>("Profit");
profit.setCellValueFactory(new PropertyValueFactory<Model, String>("profit"));
profit.setCellValueFactory(cellData -> Bindings.format("%.2f", cellData.getValue().getProfit()));
如果不格式化列,则排序是正确的。但表中并不是每次都显示两个小数。
TableColumn<Model, Float> profit = new TableColumn<Model, Float>("Profit");
profit.setCellValueFactory(new PropertyValueFactory<Model, Float>("profit"));
发布于 2018-04-25 21:01:48
使用单元格值工厂确定单元格显示的数据,并使用单元工厂确定单元格应如何显示这些数据:
TableColumn<Model, Float> profit = new TableColumn<Model, Float>("Profit");
profit.setCellValueFactory(new PropertyValueFactory<Model, Float>("profit"));
profit.setCellFactory(tc -> new TableCell<Model, Float>() {
@Override
protected void updateItem(Float profit, boolean empty) {
super.updateItem(profit, empty);
if (empty) {
setText(null);
} else {
setText(String.format("%.2f", profit.floatValue()));
}
}
});
https://stackoverflow.com/questions/50030007
复制相似问题