TableColumn 中的 JavaFX 格式加倍

JavaFX format double in TableColumn(TableColumn 中的 JavaFX 格式加倍)
本文介绍了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 格式加倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)