繁体   English   中英

JavaFX:如何将计算的Integer值添加到TableColumn

[英]JavaFX: How to add calculated Integer value to TableColumn

我有一个账单表,我想在其中列出账单上的所有产品。 我将ProductInBill对象保存在帐单上的ArrayList<ProductInBill>

创建TableView我的常用方法是创建JavaFX字段。 在控制器类上,我有我的字段:

@FXML public TableColumn<ProductInBill, String> finishedBillProductNameColumn;
@FXML public TableColumn<Integer, Integer> finishedBillProductNumberColumn;
@FXML public TableColumn<ProductInBill, Integer> finishedBillProductPriceBruttoLabel;
@FXML public TableColumn<Integer, Integer> finishedBillProductTotalAmountColumn;
@FXML public TableView finishedBillProductTable;

然后,我将setUp()方法与以下代码结合使用:

private void setUpFinishedBillProductTable() {
    finishedBillProductNameColumn.setCellValueFactory(new PropertyValueFactory<ProductInBill, String>("productName"));
    finishedBillProductPriceBruttoLabel.setCellValueFactory(new PropertyValueFactory<ProductInBill, Integer>("productPrice"));
}

还有一个updateBillTable()方法可以加载必要的ProductInBill对象,将它们保存到TableList并将其提供给表。

 private void updateFinishedBillProductTable(Bill bill) {

    LOG.info("Start reading all Products from Bill");

    for(ProductInBill product : bill.getProducts()){
          finishedBillProductCurrent.add(product);
    }
    finishedBillProductTable.getItems().clear();


    if(!finishedBillProductCurrent.isEmpty()) {
        for (ProductInBill p : finishedBillProductCurrent) {
                finishedBillProductTableList.add(p);
        }

        //here i want to calculate some other Integer values based on the ProductInBill values and insert them to the table too.  

        finishedBillProductTable.setItems(finishedBillProductTableList);
    }
}

这一切都很好。 我现在的问题是,我的TableView上还有一个字段,其中包含我不想保存在对象中的计算出的Integer值。

就拿例如finishedBillProductNumberColumn 我想在ArrayList上进行迭代,找到具有相同名称的所有产品,并将相同项目的数量填充到表中。

我怎样才能做到这一点? 我只找到了一些解决方案,在这些解决方案中,我必须使用对象中的值将某些内容插入TableView

您只需要为这些情况编写一个自定义CellValueFactory,而不是使用预制的情况。 使用PropertyValueFactory只是将成员填充单元格的便捷捷径。

例如:

 finishedBillProductNameColumn.setCellValueFactory(new PropertyValueFactory<ProductInBill, String>("productName"));

只是一种较短的方法:

finishedBillProductNameColumn.setCellValueFactory( cellData -> {
    ProductInBill productInBill = cellData.getValue();
    return data == null ? null : new SimpleStringProperty(productInBill.getProductName());
 });

就是说,我对第二种语法有100%的偏好。 因为在第一个上,如果您重命名该成员,而又忘记在那里进行更改,那么您直到在应用程序中到达那里时,才知道有一个错误。 此外,它还可以显示除成员以外的其他值。

作为您的finishedBillProductNumberColumn的具体示例,您可以执行以下操作:

首先更改定义(第一个通用类型是通过cellData.getValue()接收的类型:

@FXML public TableColumn<ProductInBill, Integer> finishedBillProductNumberColumn;

然后定义您想要的CellValueFactory:

finishedBillProductNumberColumn.setCellValueFactory( cellData -> {
    ProductInBill productInBill = cellData.getValue();

    if(productionInBill != null){
        Long nbProduct = finishedBillProductTable.getItems().stream().filter(product -> product.getProductName().equals(productInBill.getProductName())).count();

        return new SimpleIntegerProperty(nbProduct.intValue()).asObject();
    }
    return null;
});

希望能有所帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM