使用
cellValueFactory它来确定所显示的数据。单元格值工厂基本上是一个函数,该函数接受一个
CellDataFeatures对象并返回
Observablevalue要在表单元格中显示的值的包装。通常,您希望调用
getValue()该
CellDataFeatures对象以获取该行的值,然后从该对象中检索属性,就像在已发布的代码中所做的一样。
使用a
cellFactory确定如何显示这些数据。该
cellFactory是一个函数,它接受一个
TableColumn(你通常不需要),并返回一个
TableCell对象。通常,您将返回
TableCell该
updateItem()方法的子类,该子类将根据显示的新值为该单元格设置文本(有时是图形)。在您的情况下,您将价格作为
Number,并将其格式化,然后将格式化后的值传递给单元格的
setText(...)方法即可。
值得阅读相关的Javadocs:,
TableColumn.cellFactoryProperty()还可以
Cell进行单元格和单元工厂的一般性讨论。
priceProductColumn.setCellValueFactory(cellData -> cellData.getValue().priceProperty());priceProductColumn.setCellFactory(col -> new TableCell<Product, Number>() { @Override public void updateItem(Number price, boolean empty) { super.updateItem(price, empty); if (empty) { setText(null); } else { setText(String.format("US$%.2f", price.doublevalue())); } } });(我假设
priceProductColumn是a
TableColumn<Product,Number>并
Product.priceProperty()返回a
DoubleProperty。)



