0

我有带有可编辑字段项目、描述、数量、单价和小计的表格。

我正在创建一个 cellFactory 和 Column Update,如下所示:

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

}}

问题:当我更新数量列或单价列时如何更新小计列

谢谢..

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

}

}

4

1 回答 1

0

在您的 DUMMY_PurchaseOrderLine 类中创建一个名为 subTotal 的只读属性,并通过绑定在构造函数中对其进行初始化。绑定和用于设置 SubTotalColumn 值的 PropertyValueFactory 的组合将确保始终反映正确的小计。

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

请注意使用的命名约定。 使用正确的命名约定是关键

我在这里假设小计只是单行的计算值(特别是按数量 * unitPrice - 折扣),而不是跨多行计算的值的总和(这将是一个很难用 TableView 解决的问题) .


根据问题编辑更新

我从您的更新中看到您正在使用 BigDecimal 并且 JavaFX 没有相应的 BigDecimalProperty,因此您需要创建一个(如果您希望它具有全部功能,这不是微不足道的)或使用现有的属性类型之一。

您使用属性的替代方法是使用低级绑定 api来计算小计,但如果可以的话,我建议您使用属性。

于 2013-10-01T07:53:00.537 回答