我遇到一个问题,我无法从对象中加载3个属性以显示在表视图中。 我有一个付款对象,由受益对象(由3个字符串组成 - 名称,帐号和代码),金额字符串和引用字符串组成。 我可以查看这些项目是否属于同一个对象,但是当我将它们分开时,它不会导致问题而且单元格是空白的。
下面是我的课程和TableView截图。
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>
每当我尝试将付款加载到tableView时,只有金额和参考可见。 我试图使用PropertyValueFactory值的各种组合来引用我的受益对象,但没有运气。
答案 0 :(得分:0)
PropertyValueFactory
无法访问“属性的属性”。相反,您必须直接实施setCellValueFactory
所需的Callback
。您不能仅在FXML中执行此操作,但您可以使用控制器类中的lambda表达式轻松提供此实现。
// 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
}
}
或者,更简洁:
beneficiary.setCellValueFactory(cellData ->
new ReadOnlyStringWrapper(cellData.getValue().getBeneficiary().getBeneficiaryName()));