我希望尽可能短而不遗漏有用的信息。 我有以下类:如何为JavaFX设置项目TableView - 对象包含另一个对象?
public class Address{
StringProperty city = new SimpleStringProperty();
StringProperty street = new SimpleStringProperty();
//following the constructor, getters and setters
...
}
我有另一个类的客户端,即一个有一个地址构件
public class Client {
StringProperty name = new SimpleStringProperty();
StringProperty id = new SimpleStringProperty();
ObjectProperty<Address> address = new SimpleObjectProperty<>();
//following the constructor, getters and setters
...
}
和与控制器一个JavaFX接口包含TableView中对象应该在3输出列客户类的成员和给定对象的地址类的城市成员。我的TableView和TableColumn的定义是以下代码
public class SettingsController {
TableColumn<Client, String> clientNameCol;
TableColumn<Client, String> clientEmailCol;
TableColumn<Client, String> clientCityCol;
private TableView<Client> clientSettingsTableView;
...
...
clientNameCol = new TableColumn<>("Name");
clientNameCol.setCellValueFactory(new PropertyValueFactory<Client, String>("name"));
clientEmailCol = new TableColumn<>("email");
clientEmailCol.setCellValueFactory(new PropertyValueFactory<Client, String>("email"));
clientCityCol = new TableColumn<>("City");
clientCityCol.setCellValueFactory(new PropertyValueFactory<Client, String>("city"));
clientSettingsTableView.setItems(clientData);
clientSettingsTableView.getColumns().clear();
clientSettingsTableView.getColumns().addAll(clientNameCol, clientEmailCol, clientCityCol);
当然并且有一个ObservableList clientData包含客户对象的阵列。 除了应为每个客户端输出城市的列以外,一切正常。 我应该如何定义Client对象的城市(由地址成员包含)的列?
可能会有所帮助http://fxapps.blogspot.com/2012/09/showing-object-properties-in-tableview.html – invariant 2013-03-10 23:05:48