0

I have tables with editable fields item,Description,Quantity,Unit price and Sub Total.

I am creating a cellFactory and Column Update like this:

   TableColumn DescriptionCol = new TableColumn("Description");
    EditableTableSupport.createEditingColumn(DescriptionCol,"description");

    TableColumn QuantityCol = new TableColumn("Quantity");
    EditableTableSupport.createEditingColumn(QuantityCol,"quantity");

    TableColumn UnitPriceColumn = new TableColumn<>("Unit Price");
    EditableTableSupport.createEditingColumn(UnitPriceColumn,"unitPrice");

    TableColumn DiscountColumn = new TableColumn<>("Discount");
    EditableTableSupport.createEditingColumn(DiscountColumn,"discount");

    SubTotalColumn = new TableColumn<>("SubTotal");
    EditableTableSupport.createColumn(SubTotalColumn,"subTotal");

    TableColumn SubTotalColumn = new TableColumn<>("SubTotal");
    EditableTableSupport.createColumn(SubTotalColumn,"subTotal");

        DescriptionCol.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<DUMMY_PurchaseOrderLine, String>>() {
        @Override
        public void handle(TableColumn.CellEditEvent<DUMMY_PurchaseOrderLine, String> t) {
            ((DUMMY_PurchaseOrderLine) t.getTableView().getItems().get(t.getTablePosition().getRow())).setDescription(t.getNewValue());
        }
    });



    QuantityCol.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<DUMMY_PurchaseOrderLine, String>>() {
        @Override
        public void handle(TableColumn.CellEditEvent<DUMMY_PurchaseOrderLine, String> t) {
            ((DUMMY_PurchaseOrderLine) t.getTableView().getItems().get(t.getTablePosition().getRow())).setQuantity(t.getNewValue());

        }
    });

    UnitPriceColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<DUMMY_PurchaseOrderLine, String>>() {
        @Override
        public void handle(TableColumn.CellEditEvent<DUMMY_PurchaseOrderLine, String> t) {
            ((DUMMY_PurchaseOrderLine) t.getTableView().getItems().get(t.getTablePosition().getRow())).setUnitPrice(t.getNewValue());
        }
    });



    DiscountColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<DUMMY_PurchaseOrderLine, String>>() {
        @Override
        public void handle(TableColumn.CellEditEvent<DUMMY_PurchaseOrderLine, String> t) {
            ((DUMMY_PurchaseOrderLine) t.getTableView().getItems().get(t.getTablePosition().getRow())).setDiscount(t.getNewValue());
        }
    });


   public class EditableTableSupport {

   public static void createEditingColumn(TableColumn Column ,String name){


  Callback<TableColumn, TableCell> cellFactory = new Callback<TableColumn, TableCell>() {
        @Override
        public TableCell call(TableColumn p) {
            return new EditingCell();
        }
    };

    Column.setSortable(false);
    Column.setCellValueFactory(new PropertyValueFactory<DUMMY_PurchaseOrderLine, String>(name));
    Column.setCellFactory(cellFactory);

}


public static void createColumn(TableColumn Column, String name) {
    Column.setSortable(false);
    Column.setCellValueFactory(new PropertyValueFactory<DUMMY_PurchaseOrderLine, String>(name));

}}

Question:How to Update Subtotal Column When i updating Quantity Column or UnitPrice Column

Thank you..

public class DUMMY_PurchaseOrderLine {

private String name;
private String description;
private BigDecimal quantity;
private BigDecimal unitPrice;
private BigDecimal discount;
private BigDecimal subTotal;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}



public BigDecimal getQuantity() {
    return quantity;
}

public void setQuantity(BigDecimal quantity) {
    this.quantity = quantity;
}

public BigDecimal getUnitPrice() {
    return unitPrice;
}

public void setUnitPrice(BigDecimal unitPrice) {
    this.unitPrice = unitPrice;
}

public BigDecimal getDiscount() {
    return discount;
}

public void setDiscount(BigDecimal discount) {
    this.discount = discount;
}

public BigDecimal getSubTotal() {
    return subTotal;
}

public void setSubTotal(BigDecimal subTotal) {
    this.subTotal = subTotal;
}


 public DUMMY_PurchaseOrderLine(String name, BigDecimal description, BigDecimal quantity,BigDecimal unitPrice,BigDecimal discount,BigDecimal subTotal) {
    this.name = name;
    this.description = description;
    this.quantity = quantity;
    this.unitPrice = unitPrice;
    this.discount = discount;
    this.subTotal = quantity.multiply(unitPrice).subtract(discount);

}

}

1 Answers1

0

In your DUMMY_PurchaseOrderLine class create a read only property named subTotal and initialize it in the constructor via binding. The combination of the binding and the PropertyValueFactory you use to set the value for the SubTotalColumn will ensure that the correct subtotal is always reflected.

class DUMMY_PurchaseOrderLine {
    private IntegerProperty quantity  = new SimpleIntegerProperty(0);
    private DoubleProperty  unitPrice = new SimpleDoubleProperty(0);
    private DoubleProperty  discount  = new SimpleDoubleProperty(0);
    private ReadOnlyDoubleWrapper subTotal = new ReadOnlyDoubleWrapper(0);

    DUMMY_PurchaseOrderLine() { 
        subTotal.bind(quantity.multiply(unitPrice).subtract(discount));
    }

    IntegerProperty quantityProperty()  { return quantity; }
    IntegerProperty unitPriceProperty() { return unitPrice; }
    IntegerProperty discountProperty()  { return discount; }
    ReadOnlyDoubleProperty subTotalProperty() { return subTotal.getReadOnlyProperty(); }
}

Note the naming conventions used. Using the correct naming convention is key.

I'm assuming here that the subtotal is just the calculated value for a single row (specifically by quantity * unitPrice - discount), not a total of values calculated across multiple rows (which would be quite a difficult problem to solve with a TableView).


Update based on question edit

I see from your update that you are using BigDecimal and JavaFX doesn't have a corresponding BigDecimalProperty, so either you will need to create one (not trivial if you want it to be fully featured) or use one of the existing property types.

Your alternate to using properties is to use the low level binding api to calculate subtotals, but I'd advise using properties if you can.

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • i think this work only on when adding a new row to the table..QuantityCol and UnitPriceColumn are editable columns, when i click on this column and update this columns make an auto update on sub total Column (no row insertion change only in QuantityCol and SubtotalColumn or UnitPriceColumn and UnitPriceColumn ) – anoop John Oct 01 '13 at 09:02
  • @anoop I disagree, [JavaFX properties and bindings](http://docs.oracle.com/javafx/2/binding/jfxpub-binding.htm) are dynamic and can automatically recalculate values on change of dependents. If this answer does not solve your issue, then you must needs edit your question to better explain it. – jewelsea Oct 01 '13 at 09:50
  • jewelsea thanks for ur replay....my DUMMY_PurchaseOrderLine class added above, please help me to correct this... – anoop John Oct 01 '13 at 10:41
  • Use properties as I suggested in my answer, if you don't use properties, it's not going to work. – jewelsea Oct 01 '13 at 10:45