2017-10-28 10 views
0

私は請求書に載っているすべての商品をリストしたい請求表を持っています。私はProductInBillオブジェクトを請求書のArrayList<ProductInBill>に保存しました。JavaFX:TableColumnに計算された整数値を追加する方法

私がTableViewを作成したとき、私の共通のアプローチはJavaFXフィールドを作成することでした。コントローラクラスで、私は私のフィールドを持っている:

@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")); 
} 

することも必要ProductInBillオブジェクトをロードするupdateBillTable()方法があり、保存しますそれらをTableListに渡してテーブルに渡します。

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とします。私はArrayListで繰り返したい、同じ名前を持つすべての製品を見つけ、テーブルに同じアイテムの数を入力します。

どうすればいいですか?私は自分のオブジェクトから値を使って何かを私のTableViewに挿入するだけの解決策を見つけました。

答えて

1

あなたはpremadeものを使用するのではなく、それらのケースに対してカスタムCellValueFactoryを書くだけでよいのです。 PropertyValueFactoryを使用すると、メンバーをセルに埋め込むのに便利です。ご例えば

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

を行うにはちょうど短い方法です:

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

言われていること、私は2番目の構文については、100%の好みを持っています。最初のメンバーでメンバーの名前を変更してそのメンバーを変更するのを忘れた場合、アプリケーションに参加するまで間違いがあることはわかりません。それだけでなく、メンバーとは異なる値を表示することができます。以下のための具体的な例として

finishedBillProductNumberColumnあなたが行うことができます:

まず最初に一般的なタイプはcellData.getValue()で受信した1つである(定義を変更:

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

はそれが助けを願って

関連する問題