我尝试从对象列表中加载 Tableview 组件,在我的案例中是客户端对象。客户端对象包含地址对象和反之亦然。 客户端和地址对象定义如下;
public class Client{
private String firstName;
private String lastName;
private String emailAddress;
private String phoneNumber;
private Address clientsAddress;
public Client(){
//blah blah
}
//getters
...
//setters
...
}
public class Address{
//address lines + county
private String a1;
private String a2;
private String a3;
private String county;
private Client client;
public Address(){
//blah blah
}
//getters
...
//setters
...
}
我可以将客户端字段显示在表视图中,将客户端Object中的字段名称映射到列。
final TableView<Client> tableView = new TableView<>();
final TableColumn firstNameCol = new TableColumn("First Name");
final TableColumn lastNameCol = new TableColumn("Last Name");
final TableColumn emailCol = new TableColumn("Email Address");
final TableColumn addressCol1 = new TableColumn("Address Line 1");
final TableColumn addressCol2 = new TableColumn("Address Line 2");
firstNameCol.setMinWidth(90);
firstNameCol.setMaxWidth(110);
firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
lastNameCol.setMinWidth(90);
lastNameCol.setMaxWidth(110);
lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
emailCol.setMinWidth(100);
emailCol.setMaxWidth(210);
emailCol.setCellValueFactory(new PropertyValueFactory<>("emailAddress"));
//Don't know how to access the Address Object inside Client Object in Next two parts
addressColumn1.setMinWidth(100);
addressColumn1.setMaxWidth(200);
addressColumn1.setCellValueFactory(new PropertyValueFactory<>(""));
adddessColumn2.setMinWidth(100);
addressColumn2.setMaxWidth(200);
addressColumn2.setCellValueFactory(new PropertyValueFactory<>(""));
tableView.getColumns().addAll(firstNameCol, lastNameCol, emailCol, addressColumn1, addressColumn2);
我的问题是,是否有任何方法可以访问客户端对象中的地址对象,或者将Address对象中的字段名称映射到表列的任何方式。任何帮助将不胜感激,谢谢。
答案 0 :(得分:2)
感谢您的提示,我通过覆盖能够访问客户端对象的调用方法来实现它。然后我能够访问地址对象并返回 SimpleStringProperty 并相应地更新每一列。
addressColumn1.setCellValueFactory(new Callback<CellDataFeatures<Client, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(CellDataFeatures<Client, String> c) {
return new SimpleStringProperty(c.getValue().getAddress().getA1());
}
});