我有一个TableView<Marriage>
控制器类:
public class MarriageTableView extends AbstractTableViewController {
@FXML
private TableView<Marriage> tableView;
@FXML
private TableColumn<Groom, String> groomFirstNameColumn, groomLastNameColumn;
@FXML
private TableColumn<Bride, String> brideFirstNameColumn, brideLastNameColumn;
@FXML
private TableColumn<Marriage, LocalDate> marriageDateColumn;
@FXML
void initialize() {
this.groomFirstNameColumn.setCellValueFactory(new PropertyValueFactory<Groom, String>("firstName")); // <- Exception here
this.groomLastNameColumn.setCellValueFactory(new PropertyValueFactory<Groom, String>("lastName"));
this.brideFirstNameColumn.setCellValueFactory(new PropertyValueFactory<Bride, String>("firstName"));
this.brideLastNameColumn.setCellValueFactory(new PropertyValueFactory<Bride, String>("lastName"));
this.marriageDateColumn.setCellValueFactory(new PropertyValueFactory<Marriage, LocalDate>("marriageDate"));
}
在initialize
方法中,我试图CellValueFactory
为每个表格列设置,所以我可以Marriage
在我的表格视图中显示对象。
然而,Marriage 对象由另一个类型为 Groom 和 Bride 的对象组成,我试图从中显示一些值。
public class Marriage {
private Groom groom;
private Bride bride;
private LocalDate marriageDate;
}
public class Groom:
{
private String firstName;
private String lastName;
}
public class Bride:
{
private String firstName;
private String lastName;
}
我得到的例外:
java.lang.IllegalStateException: Cannot read from unreadable property firstName
如何设置CellValueFactory
或编辑TableColumn
的通用参数,以便显示嵌套对象的属性?