0
votes

I have an issue where I can't load up 3 properties from an object to show up on a table view. I have a payment object that is made up of a beneficiary object (consisting of 3 strings - name, accountnumber and code), an amount string and a reference string. I can view the items if they were all part of the same object, however when I split them up, Its not causing issues and the cell is blank.

Below are my classes and the TableView screenshot.

public class Payment {

private Beneficiary beneficiary;
private String amount;
private String reference;

public void setBeneficiary(Beneficiary bene) {
    beneficiary = bene;
}

public Beneficiary getBeneficiary() {
    return beneficiary;
}

public void setAmount(String amt) {
    amount = amt;
}

public String getAmount() {
    return amount;
}

public void setReference(String ref) {
    reference = ref;
}

public String getReference() {
    return reference;
}



@XStreamAlias("beneficiary")
public class Beneficiary {

@XStreamAlias("beneficiaryName")
private String beneficiaryName;
@XStreamAlias("beneficiaryAccountNumber")
private String beneficiaryAccountNumber;
@XStreamAlias("beneficiarySortCode")
private String beneficiarySortCode;

public String getBeneficiaryName() {
    return beneficiaryName;
}

public void setBeneficiaryName(String name) {
    beneficiaryName = name;
}

public String getBeneficiaryAccountNumber() {
    return beneficiaryAccountNumber;
}

public void setBeneficiaryAccountNumber(String accountNumber) {
    beneficiaryAccountNumber = accountNumber;
}

public String getBeneficiarySortCode() {
    return beneficiarySortCode;
}

public void setBeneficiarySortCode(String sortCode) {
    beneficiarySortCode = sortCode;
}



        <TableView fx:id="paymentTable" layoutX="17.0" layoutY="222.0" prefHeight="200.0" prefWidth="812.0">
          <columns>
            <TableColumn fx:id="beneficiaryNameColumn" editable="false" prefWidth="169.0" text="Beneficiary Name">
                <cellValueFactory><PropertyValueFactory property="beneficiaryName" /></cellValueFactory>
            </TableColumn>
            <TableColumn fx:id="beneficiaryAccountNumberColumn" prefWidth="177.0" text="Beneficiary Account Number">
                <cellValueFactory><PropertyValueFactory property="beneficiaryAccountNumber" /></cellValueFactory>
            </TableColumn>
            <TableColumn fx:id="beneficiarySortCodeColumn" prefWidth="130.0" text="Beneficiary Sort Code">
                <cellValueFactory><PropertyValueFactory property="beneficiarySortCode" /></cellValueFactory>
            </TableColumn>
            <TableColumn fx:id="amount" prefWidth="147.0" text="Amount">
                <cellValueFactory><PropertyValueFactory property="amount" /></cellValueFactory>
            </TableColumn>
            <TableColumn prefWidth="184.0" text="Payment Reference">
                <cellValueFactory><PropertyValueFactory property="reference" /></cellValueFactory>
            </TableColumn>
          </columns>
        </TableView>

Everytime I try to load a payment to the tableView, only amount and reference are visible. I have tried to use various combinations for the PropertyValueFactory value to reference my beneficiary object but no luck.

enter image description here

1

1 Answers

0
votes

A PropertyValueFactory cannot access a "property of a property". Instead, you must implement the Callback required by setCellValueFactory directly. You cannot do this solely in FXML, but you can easily provide this implementation using a lambda expression in the controller class.

// controller class referenced by root element of FXML file:
public class MyController {

    public void initialize() {

        beneficiaryNameColumn.setCellValueFactory(cellData -> {
            Payment payment = cellData.getValue();
            Beneficiary beneficiary = payment.getBeneficiary();
            return new ReadOnlyStringWrapper(beneficiary.getBeneficiaryName());
        });

        // etc
    }

}

Or, more concisely:

beneficiary.setCellValueFactory(cellData ->
    new ReadOnlyStringWrapper(cellData.getValue().getBeneficiary().getBeneficiaryName()));