2

I am developing a Food Calculator which recalculates food elements (like Fat, Protein, Carbohydrates) after a TableColumn cell has been modified.

The modification should be only visual so I decided to change the values, then refresh the TableView and then put the old values back without refreshing again so that the user sees the new values but backend data stays the same.

This is how I did it:

enter image description here

Here is the text form for copy/paste purposes:

for(TableColumn<Food, Float> n : columnArray) {
    n.setCellFactory(TextFieldTableCell.forTableColumn(new FloatStringConverter()));

    n.setOnEditCommit(e -> {
        float oldValue = e.getOldValue();
        float newValue = e.getNewValue();

        float proportionalChange = newValue / oldValue;

        float quantityOldValue = e.getRowValue().getQuantity();
        float proteinOldValue = e.getRowValue().getProtein();
        float carbohydratesOldValue = e.getRowValue().getCarbohydrates();
        float fatOldValue = e.getRowValue().getFat();

        e.getRowValue().setQuantity(proportionalChange * quantityOldValue);
        e.getRowValue().setProtein(proportionalChange * proteinOldValue);
        e.getRowValue().setCarbohydrates(proportionalChange * carbohydratesOldValue);
        e.getRowValue().setFat(proportionalChange * fatOldValue);

        foodTable.refresh();

        e.getRowValue().setQuantity(quantityOldValue);
        e.getRowValue().setProtein(proteinOldValue);
        e.getRowValue().setCarbohydrates(carbohydratesOldValue);
        e.getRowValue().setFat(fatOldValue);                


    });
}

The problem is - calling setQuantity and other methods at the end of this code refreshes the TableView automatically. Is there a way to stop it or update them another way?

NakedCat
  • 852
  • 1
  • 11
  • 40

0 Answers0