javafx:将属性绑定在一起并在tableview

时间:2015-08-15 06:05:04

标签: java javafx

我有以下类,其中包含价格,数量属性,这两个属性的任何更改都应导致交易费用的更新。

public class GroceryCounter {


    private DoubleProperty volume;
    private DoubleProperty price;
    private DoubleBinding transactionFee;

    public double getVolume(){
        return this.volume.get();
    }

    public DoubleProperty volumeProperty(){
        return this.volume;
    }

    public void setVolume(double volume){
        this.volume.set(volume);
    }


    public double getPrice(){
        return this.price.get();
    }   

    public DoubleProperty priceProperty(){
        return this.price;
    }

    public void setPrice(double price){
        this.price.set(price);
    }

    public double getTransactionFee(){
            this.transactionFee = this.price.multiply(this.volume).multiply(0.0025);
            return this.transactionFee.getValue();
    }

    public DoubleBinding transactionFeeProperty(){
        return this.transactionFee;
    }
}

这是我在Controller课程中使用的代码。

public TableColumn<Trade, Double> transactionFeeColumn;
......

 // in the initialize method
 transactionFeeColumn.setCellValueFactory(cellData -> cellData.getValue().transactionFeeProperty());   

问题

  1. 我未能在表格列上显示交易费用,并显示以下消息:

    类型不匹配:无法从DoubleBinding转换为ObservableValue

  2. 我觉得上面的代码非常不安全,因为我觉得我应该使用getTransactionFee()代替transactionFeeProperty()来在tableColumn中显示结果。

    1. 我是否正确地将price属性和volume属性绑定到transactionFee
    2. 感谢。

1 个答案:

答案 0 :(得分:1)

改变:

public TableColumn<Trade, Double> transactionFeeColumn;

为:

public TableColumn<GroceryCounter, Number> transactionFeeColumn;

为什么呢?因为: DoubleBinding类实现ObservableValue<Number>接口。

您的模型类应如下所示:

    public class GroceryCounter  {

    private final DoubleProperty volume = new SimpleDoubleProperty();
    private final DoubleProperty price = new SimpleDoubleProperty();
    private  final DoubleBinding transactionFee;

    public GroceryCounter () {
        this.transactionFee = this.price.multiply(this.volume).multiply(0.0025);
    }



    public double getVolume() {
        return this.volume.get();
    }

    public DoubleProperty volumeProperty() {
        return this.volume;
    }

    public void setVolume(double volume) {
        this.volume.set(volume);
    }

    public double getPrice() {
        return this.price.get();
    }

    public DoubleProperty priceProperty() {
        return this.price;
    }

    public void setPrice(double price) {
        this.price.set(price);
    }

    public double getTransactionFee() {

            return this.transactionFee.getValue();
    }

    public DoubleBinding transactionFeeProperty() {
        return this.transactionFee;
    }
}

<强>更新

以下是一个例子:

public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        TableColumn<GroceryCounter, Number> TransCol = new TableColumn("transactionFee");
        TransCol.setCellValueFactory(cellData -> cellData.getValue().transactionFeeProperty());
        TableColumn<GroceryCounter, Number> priceCol = new TableColumn("price");
        priceCol.setCellValueFactory(cellData -> cellData.getValue().priceProperty());
        TableColumn<GroceryCounter, Number> volumeCol = new TableColumn("volume");
        volumeCol.setCellValueFactory(cellData -> cellData.getValue().volumeProperty());
        GroceryCounter item = new GroceryCounter();
        item.setPrice(10);
        item.setVolume(10);
        TableView<GroceryCounter> table = new TableView();
        table.getColumns().addAll(volumeCol, priceCol, TransCol);
        table.getItems().add(item);
        AnchorPane root = new AnchorPane();
        root.getChildren().add(table);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String... args) {
        Application.launch(args);
    }
}