JavaFX: Dynamic Boolean Binding in RowFactory

729 Views Asked by At

This question goes further where JavaFX: BooleanBindings in bind with multiple EasyBind maps stopped.

I would like to extend the row factory a bit further: In table 1 the products are presented with 1 piece a row, so there could be multiple rows for 1 product object. Now I only want to disable a row, if the amount in table 2 is equal to the summed amounts of the rows. For example: Product(SKU: P1) has three lines in table1 (all representing amount 1). If Product(SKU: P1) had amount = -2 in table2, 2 rows of this product object need to be disabled in table1. One still needs to be enabled, doesn't matter which one of the three.

I have the following code at the moment:

table1.setRowFactory(tv -> {
    TableRow<Product> row = new TableRow<>();        
    row.disableProperty().bind(Bindings.createBooleanBinding(
    () -> row.getItem() != null && (row.getItem().getSKU().startsWith("C") ||
        (skuAmountLookup.containsKey(row.getItem().getSKU()) &&
            -skuAmountLookup.get(row.getItem().getSKU()) <
            amounts[SKUs.indexOf(row.getItem().getSKU())] )), 
        skuAmountLookup,
        row.itemProperty()));   
    return row;
});

where skuAmountLookup is:

skuAmountLookup = table2.getItems().stream().collect(
    Collectors.toMap(Product::getSKU, Product::getAmount, (x, y) -> y,
        () -> FXCollections.observableHashMap()));

This only disables the rows when the total amount is table 1. You could change amounts[SKUs.indexOf(row.getItem().getSKU())] to 0, which will disable all the rows when only one of the products is present.

My question is: how can I change the row factory in such a way that the disableProperty of another row is taken into account?

For additional information, see: JavaFX: BooleanBindings in bind with multiple EasyBind maps and JavaFX: Disable multiple rows in TableView based on other TableView.

0

There are 0 best solutions below