![](/img/trans.png)
[英]JavaFX: Make TableColumn observe formatted Double with extractor functionality
[英]JavaFX format double in TableColumn
TableColumn<Product, Double> priceCol = new TableColumn<Product,Double>("Price");
priceCol.setCellValueFactory(new PropertyValueFactory<Product, Double>("price"));
如何在此列中将双精度格式设置为小数点后两位(因为它们是价格列)?默认情况下,它们仅显示小数点后一位。
使用单元格工厂来生成使用货币格式化程序来格式化显示的文本的单元格。 这意味着价格将在当前语言环境中格式化为一种货币格式(即使用本地货币符号和小数位数的适当规则等)。
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
priceCol.setCellFactory(tc -> new TableCell<Product, Double>() {
@Override
protected void updateItem(Double price, boolean empty) {
super.updateItem(price, empty);
if (empty) {
setText(null);
} else {
setText(currencyFormat.format(price));
}
}
});
请注意,这是您已经在使用的cellValueFactory
之外的cellValueFactory
。 cellValueFactory
确定在单元格中显示的值。 cellFactory
确定定义如何显示它的单元格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.