私は有用な情報を省略することなくできるだけ短くしたい。 私は、次のクラスがあります。私は別のクラスのクライアントがJavaFX TableViewの項目を設定する方法 - オブジェクトに別のオブジェクトが含まれていますか?
public class Address{
StringProperty city = new SimpleStringProperty();
StringProperty street = new SimpleStringProperty();
//following the constructor, getters and setters
...
}
を、1つはアドレス部材
public class Client {
StringProperty name = new SimpleStringProperty();
StringProperty id = new SimpleStringProperty();
ObjectProperty<Address> address = new SimpleObjectProperty<>();
//following the constructor, getters and setters
...
}
及び3でなければならない出力テーブルビューオブジェクトを含むコントローラとJavaFXのインタフェースを有しています指定されたオブジェクトのClientクラスのメンバとAddressクラスのメンバを列にします。私のテーブルビューと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オブジェクトの配列が含まれていることがあります。 各クライアントの都市を出力する列以外はすべて正常に動作します。 Clientオブジェクトの都市(Addressメンバーに含まれる)の列を定義するにはどうすればよいですか? (
clientCityCol = new TableColumn<>("City");
clientCityCol.setCellValueFactory(new PropertyValueFactory<Client, Address>("address"));
// ======== setting the cell factory for the city column
clientCityCol.setCellFactory(new Callback<TableColumn<Client, Address>, TableCell<Client, Address>>(){
@Override
public TableCell<Client, Address> call(TableColumn<Client, Address> param) {
TableCell<Client, Address> cityCell = new TableCell<Client, Address>(){
@Override
protected void updateItem(Address item, boolean empty) {
if (item != null) {
Label cityLabel = new Label(item.getCity());
setGraphic(cityLabel);
}
}
};
return cityCell;
}
});
アドレスクラスはゲッターgetCityを(持っている)文字列として都市メンバーを返します。あなたの助けのための
参考になる可能性があります。http://fxapps.blogspot.com/2012/09/showing-object-properties-in-tableview.html – invariant