JavaFX format double in TableColumn(TableColumn 中的 JavaFX 格式加倍)
问题描述
TableColumn<Product, Double> priceCol = new TableColumn<Product,Double>("Price");
priceCol.setCellValueFactory(new PropertyValueFactory<Product, Double>("price"));
如何将此列中的双精度设置为具有 2 个小数位(因为它们是价格列)?默认情况下它们只显示 1 个小数位.
How do I format the doubles in this column to have 2 decimal places(Because they are the column for price)?By default they only show 1 decimal place.
推荐答案
使用生成单元格的单元格工厂,该单元格使用货币格式化程序来格式化显示的文本.这意味着价格将被格式化为当前语言环境中的货币(即使用当地货币符号和适当的小数位数规则等).
Use a cell factory that generates cells that use a currency formatter to format the text that is displayed. This means the price will be formatted as a currency in the current locale (i.e. using the local currency symbol and appropriate rules for number of decimal places, etc.).
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
确定单元格中显示的值;cellFactory
确定定义如何显示它的单元格.
Note this is in addition to the cellValueFactory
you are already using. The cellValueFactory
determines the value that is displayed in a cell; the cellFactory
determines the cell that defines how to display it.
这篇关于TableColumn 中的 JavaFX 格式加倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:TableColumn 中的 JavaFX 格式加倍


- 转换 ldap 日期 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 获取数字的最后一位 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01