JavaFX表不接受数据

时间:2016-07-27 19:16:02

标签: javafx tableview javafx-8

这是我的表视图类(不是FX类,我自己的数据)

class tableView {

SimpleIntegerProperty profit;
SimpleDoubleProperty probabilty;

public tableView(int profit, double prob) {
    this.profit = new SimpleIntegerProperty(profit);
    this.probabilty = new SimpleDoubleProperty(prob);
}
}

和我的事件处理程序和其他控制器的代码是:

    Random r = new Random();
    int noOfStream = Integer.parseInt(steamsize.getText());
    double sellingPrice = Double.valueOf(sell.getText());
    double costPrice = Double.valueOf(cost.getText());
    int quantity = Integer.valueOf(Quantity.getText());
    double discountPrice = Double.valueOf(discount.getText());
    int randomStart = Integer.parseInt(RandStart.getText());
    int randomEnd = Integer.parseInt(RandEnd.getText());

    for (int i = 0; i < noOfStream; i++) {
        int dem = r.nextInt(randomEnd - randomStart) + randomStart;
        int profit = profitCacl(sellingPrice, costPrice, quantity, discountPrice, dem);
        double prob = cal.probCal(profitArray, profit);
      //  System.out.println(profit);
      //  System.out.println(prob);
        if (prob == 0.1) {
            profitArray.add(new tableView(profit, prob));
        } 
    }

}

@Override
public void initialize(URL url, ResourceBundle rb) {

    cal = new methodsForCal();
    choice.setItems(FXCollections.observableArrayList(
            "Normal",
            "Poison",
            "Uniform"
    ));

    Profit.setCellValueFactory(new PropertyValueFactory("profit"));
    Probabilty.setCellValueFactory(new PropertyValueFactory("probabilty"));

    profitArray = FXCollections.observableArrayList();

    tableForReading.setItems(profitArray);

}

现在我的问题是我的数据没有插入数据!因为它在控制台上打印就像我想要的一样好!

1 个答案:

答案 0 :(得分:0)

PropertyValueFactory要求item类包含返回属性值的属性或getter的方法。您需要将方法profitPropertyprobabiltyProperty添加到tableView,以返回属性。

或者,您可以使用自己的cellValueFactory s:

// assuming TableColumn declarations with type parameters
Profit.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<tableView, Number>, ObservableValue<Number>>() {

    @Override
    public ObservableValue<Number> call(TableColumn.CellDataFeatures<tableView, Number> param) {
        return param.getValue().profit;
    }
});
Probabilty.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<tableView, Number>, ObservableValue<Number>>() {

    @Override
    public ObservableValue<Number> call(TableColumn.CellDataFeatures<tableView, Number> param) {
        return param.getValue().probability;
    }
});

模式信息可在此处找到:https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/table-view.htm