6

有益な情報を省略せずに、できるだけ短くしたい。私は次のクラスを持っています:

public class Address{
StringProperty city = new SimpleStringProperty();
StringProperty street = new SimpleStringProperty();

//following the constructor, getters and setters
...
}

別のクラス Client があり、そのクラスには Address メンバーがあります

public class Client {

StringProperty name = new SimpleStringProperty();
StringProperty  id = new SimpleStringProperty();
ObjectProperty<Address> address = new SimpleObjectProperty<>();

//following the constructor, getters and setters
...
}

そして、指定されたオブジェクトの Client クラスのメンバーと Address クラスの都市メンバーを 3 列に出力する必要がある TableView オブジェクトを含むコントローラーを備えた JavaFX インターフェイス。私の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);

そしてもちろん、Client オブジェクトの配列を含む ObservableList clientData があります。各クライアントの都市を出力する必要がある列を除いて、すべてが正常に機能します。Client オブジェクトの都市 (Address メンバーに含まれる) の列を定義するにはどうすればよいですか?

4

3 に答える 3

7

@invariantご協力ありがとうございます。もう少しグーグルで検索したところ、次の解決策が得られました。

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;                
        }

    });

Address クラスには、市のメンバーを String() として返すゲッター getCity() があります。

于 2013-03-11T18:22:19.357 に答える
0

clientCityCol の定義を変更する必要があることを忘れていました。そうTableColumn<Client, String> clientCityCol;しないと機能しTableColumn<Client, Address> clientCityCol;ません。

于 2016-10-27T03:23:07.463 に答える