簡體   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