2

I have a Table Witch look like the below table

TableVeiw<Transaction>

 ---------------------------------------------------------------------
| id | Transaction date |  Name | type  | Debit Amount | Credit Amount|
|---------------------------------------------------------------------|
| 1  |   21/02/2016     |Invoice|Credit |              |      12000   |
|---------------------------------------------------------------------|
| 2  |    21/02/2016    |Payment|Debit  |  20000       |              |
|---------------------------------------------------------------------|
                                        |  Total Debit | Total Credit | 
                                         -----------------------------

The data in Debit amount and Credit amount come from one property of Transaction Object the code snnipet of how to populate those columns is below:

 tcCreditAmmout.setCellValueFactory(cellData -> {
            Transaction transaction = cellData.getValue() ;
            BigDecimal value = null;
            if(transaction.getKindOfTransaction() == KindOfTransaction.CREDIT){
                value = transaction.getAmountOfTransaction();
            }

            return new ReadOnlyObjectWrapper<BigDecimal>(value);
        });

        tcDebitAmmout.setCellValueFactory(cellData -> {
            Transaction transaction = cellData.getValue() ;
            BigDecimal value = null;
            if(transaction.getKindOfTransaction() == KindOfTransaction.DEBIT){
                value = transaction.getAmountOfTransaction();
            }

            return new ReadOnlyObjectWrapper<BigDecimal>(value);
        });

I need to calculate the total of :Total Debit(See the above table) and Total Credit (See the above table) every time the TableView item changes via Javafx Bindings, but i have no idea how to acheive this.

Note: Total Debit and Total Credit are Labels ,

abdou amer
  • 819
  • 2
  • 16
  • 43
  • How is this different to the second part of http://stackoverflow.com/questions/35187145/javafx-tableview-items-prevent-duplicates? You seem to be repeatedly asking the same question. – James_D Feb 21 '16 at 22:36
  • No, it's diffrent, transaction.getAmountOfTransaction() is splited into columns based on the type of transaction. I need to calculate the sum for each column tcCreditAmmout and tcDebitAmmout – abdou amer Feb 21 '16 at 22:40
  • But these are just the sums of `transaction.getAmountOfTransaction()` for all the transactions that have `transaction.getKindOfTransaction() == KindOfTransaction.CREDIT` and `transaction.getKindOfTransaction() == KindOfTransaction.DEBIT`, respectively, aren't they? – James_D Feb 21 '16 at 22:45
  • Yes, But i need to seprate them sum of transaction with type CREDIT and sum of transaction with type DEBIT. – abdou amer Feb 21 '16 at 22:49
  • Not really sure I see why that is so different, but see answer. – James_D Feb 21 '16 at 23:16

1 Answers1

3

Assuming you have

TableView<Transaction> table = ... ;
Label totalDebit = ... ;
Label totalCredit = ... ;

then you just need:

totalDebit.textProperty().bind(Bindings.createObjectBinding(() -> 
    table.getItems().stream()
         .filter(transaction -> transaction.getKindOfTransaction() == KindOfTransaction.DEBIT)
         .map(Transaction::getAmountOfTransaction)
         .reduce(BigDecimal.ZERO, BigDecimal::add),
         table.getItems())
    .asString("%.3f"));

and of course

totalCredit.textProperty().bind(Bindings.createObjectBinding(() -> 
    table.getItems().stream()
         .filter(transaction -> transaction.getKindOfTransaction() == KindOfTransaction.CREDIT)
         .map(Transaction::getAmountOfTransaction)
         .reduce(BigDecimal.ZERO, BigDecimal::add),
         table.getItems())
    .asString("%.3f"));

If getAmountOfTransaction might change while the transaction is part of the table, then your table's items list must be constructed with an extractor.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • I think, there is a typo in your code: you have to use `Bindings.createObjectBinding()` instead of `Bindings.computeObjectBinding()` – Kachna Feb 23 '16 at 10:21
  • Yes, sorry, not sure how that typo happened. Edited to fix. – James_D Feb 23 '16 at 10:33
  • For: table.getItems()) .asString("%.3f")); , how can i customize the pattern ? – abdou amer Feb 26 '16 at 19:33
  • Just read the docs: [`asString()`](http://docs.oracle.com/javase/8/javafx/api/javafx/beans/binding/ObjectExpression.html#asString-java.lang.String-) which references [`java.util.Formatter`](http://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html) – James_D Feb 26 '16 at 19:36