简体   繁体   中英

JavaFX: How to add calculated Integer value to TableColumn

I have a bill table where I want to list all products which are on the bill. I saved the ProductInBill objects within an ArrayList<ProductInBill> on the bill.

When I created a TableView my common approach is to create the JavaFX fields. On the controller class, I have my fields:

@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;

Then I am using a setUp() method with the code like:

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

Also there is an updateBillTable() method to load the necessary ProductInBill objects, save them to an TableList and give it to the table.

 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);
    }
}

This is all working very good. My problem now is, that I have also a field on my TableView with calculated Integer values which I don't want to save within an object.

Take for example the finishedBillProductNumberColumn . I want iterate on my ArrayList , find all products with the same name and populate the number of the same items to the table.

How can I do this? I found only solutions where I have to use a value from my object to insert something to my TableView .

You just have to write a custom CellValueFactory for those case instead of using premade ones. Using PropertyValueFactory is just an handy short cut to fill cells with members.

For your example:

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

is just a shorter way to do:

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

That being said, i have an 100% preference for the second syntax. Because on the first one if you rename the member, and you forgot to change it there, you won't know there is a mistake until you get there in the application. Plus it allow to display different value than just the members.

As a concrete example for your finishedBillProductNumberColumn you could do:

First change the definition(the first Generic type is the one received with cellData.getValue() :

@FXML public TableColumn<ProductInBill, Integer> finishedBillProductNumberColumn;

and then define the CellValueFactory you want like:

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;
});

Hope it helped!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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