JavaFX:如何将计算的Integer值添加到TableColumn

时间:2017-10-28 13:17:35

标签: java javafx javafx-8

我有一张账单表,我想列出账单上的所有产品。我在帐单var d = new Array(0,0,0,0,0); function aaa() { var i = 0 alert( "hello" ); for(i; i<5; i++){ var randomn = Math.random(); var da = Math.floor(randomn*6) +1; var elid = "d" + i; var di = document.getElementById(elid); di.src = "pix/dice" + d + ".gif"; } } function newGame(){ } function click1() { var total = 0; for (var i = 0; i <5; i++){ if (d[i] == 1 ){total +=1;} var loco = document.getElementById("s1"); loco.innerHTML = total; } }内保存了<h1>Yahtzee</h1> <div style = "text-align:center;"> <img src = "pix/dice0.gif" id = "d0"> <img src = "pix/dice0.gif" id = "d1"> <img src = "pix/dice0.gif" id = "d2"> <img src = "pix/dice0.gif" id = "d3"> <img src = "pix/dice0.gif" id = "d4"> </div> <table border =2 align =center> <tr> <td class = "c1"> 1's </td> <td class = "c2"><div class = "scr" id = "s1" onclick= "click1()" > </div> </td> <td class = "c3" rowspan = "6"><button id = "roll;" onclick = "aaa();">Roll</button> </td> </tr> <tr> <td class = "c1"> 2's </td> <td class = "c2"><div class = "scr" id = "s2"> </div> </td> </tr> <tr> <td class = "c1"> 3's </td> <td class = "c2"><div class = "scr" id = "s3"> </div> </td> </tr> <tr> <td class = "c1"> 4's </td> <td class = "c2"><div class = "scr" id = "s4"> </div> </td> </tr> <tr> <td class = "c1"> 5's </td> <td class = "c2"><div class = "scr" id = "s5"> </div> </td> </tr> <tr> <td class = "c1"> 6's </td> <td class = "c2"><div class = "scr" id = "s6"> </div> </td> </tr> <tr> <td class = "c1"> 3 of a kind </td> <td class = "c2"><div class = "scr" id = "k3"> </div> </td> <td class = "c3" rowspan = "8"><button id = "ng" onclick="newGame()"; >New Game </button> </td> </tr> <tr> <td class = "c1"> 4 of a kind </td> <td class = "c2"><div class = "scr" id = "k4"> </div> </td> </tr> <tr> <td class = "c1"> Full House </td> <td class = "c2"><div class = "scr" id = "fh"> </div> </td> </tr> <tr> <td class = "c1"> Sm Straight </td> <td class = "c2"><div class = "scr" id = "ss"> </div> </td> </tr> <tr> <td class = "c1"> Lg Straight </td> <td class = "c2"><div class = "scr" id = "ls"> </div></td> </tr> <tr> <td class = "c1"> Yahtzee </td> <td class = "c2"><div class = "scr" id = "yah"> </div> </td> </tr> <tr> <td class = "c1"> Chance </td> <td class = "c2"><div class = "scr" id = "cha"> </div> </td> </tr> <tr> <td class = "c1"> Total</td> <td class = "c2"><div class = "scr" id = "tot"> </div> </td> </tr> </table>个对象。

当我创建ProductInBill时,我的常用方法是创建JavaFX字段。在控制器类上,我有我的字段:

ArrayList<ProductInBill>

然后我使用TableView方法,代码如下:

@FXML public TableColumn<ProductInBill, String> finishedBillProductNameColumn;
@FXML public TableColumn<Integer, Integer> finishedBillProductNumberColumn;
@FXML public TableColumn<ProductInBill, Integer> finishedBillProductPriceBruttoLabel;
@FXML public TableColumn<Integer, Integer> finishedBillProductTotalAmountColumn;
@FXML public TableView finishedBillProductTable;

还有一个setUp()方法来加载必要的private void setUpFinishedBillProductTable() { finishedBillProductNameColumn.setCellValueFactory(new PropertyValueFactory<ProductInBill, String>("productName")); finishedBillProductPriceBruttoLabel.setCellValueFactory(new PropertyValueFactory<ProductInBill, Integer>("productPrice")); } 对象,将它们保存到TableList并将其提供给表。

updateBillTable()

这一切都很好。我现在的问题是,我的ProductInBill上还有一个字段,其中包含计算出的Integer值,我不想在对象中保存。

private void updateFinishedBillProductTable(Bill bill) { LOG.info("Start reading all Products from Bill"); for(ProductInBill product : bill.getProducts()){ finishedBillProductCurrent.add(product); } finishedBillProductTable.getItems().clear(); if(!finishedBillProductCurrent.isEmpty()) { for (ProductInBill p : finishedBillProductCurrent) { finishedBillProductTableList.add(p); } //here i want to calculate some other Integer values based on the ProductInBill values and insert them to the table too. finishedBillProductTable.setItems(finishedBillProductTableList); } } 为例。我想在我的TableView上进行迭代,找到所有具有相同名称的产品,并将相同项目的数量填充到表格中。

我该怎么做?我发现只有解决方案,我必须使用我的对象的值来插入我的finishedBillProductNumberColumn

1 个答案:

答案 0 :(得分:1)

您只需为这些案例编写自定义CellValueFactory,而不是使用预制的CellValueFactory。使用PropertyValueFactory只是一个方便的捷径来填充成员的细胞。

对于你的例子:

 finishedBillProductNameColumn.setCellValueFactory(new PropertyValueFactory<ProductInBill, String>("productName"));

只是一种较短的方式:

finishedBillProductNameColumn.setCellValueFactory( cellData -> {
    ProductInBill productInBill = cellData.getValue();
    return data == null ? null : new SimpleStringProperty(productInBill.getProductName());
 });

话虽如此,我对第二种语法有100%的偏好。因为在第一个,如果你重命名该成员,并且你忘了在那里更改它,你不会知道在你到达应用程序之前有一个错误。此外,它允许显示与成员不同的价值。

作为finishedBillProductNumberColumn的一个具体示例,您可以这样做:

首先更改定义(第一个通用类型是使用cellData.getValue()收到的类型:

@FXML public TableColumn<ProductInBill, Integer> finishedBillProductNumberColumn;

然后定义你想要的CellValueFactory:

finishedBillProductNumberColumn.setCellValueFactory( cellData -> {
    ProductInBill productInBill = cellData.getValue();

    if(productionInBill != null){
        Long nbProduct = finishedBillProductTable.getItems().stream().filter(product -> product.getProductName().equals(productInBill.getProductName())).count();

        return new SimpleIntegerProperty(nbProduct.intValue()).asObject();
    }
    return null;
});

希望它有所帮助!