How to set elements for JavaFX TableView - object contains another object?

I want to be as short as possible without omitting useful information. I have the following class:

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

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

I have another class client that has an address member

public class Client {

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

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

and a JavaFX interface with a controller that contains the TableView object, which should display in 3 columns the members of the Client class and the city member of the Address class for this object. My definition of TableView and TableColumn is the following code

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

and, of course, there is a CustomerData ObservableList that contains an array of the Client object. Everything works fine, except for the column, which should display the city for each client. How to determine the city column (contained in the Address element) of the Client object?

+5
source share
3

@invariant , googled , :

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

    });

getter getCity(), String().

+6

fxml?

:

<TableColumn prefWidth="8" text="City"> <cellValueFactory > <PropertyValueFactory property="adress.city" /> </cellValueFactory> </TableColumn>

, .

0

You forgot to mention that you need to change the definition of clientCityCol TableColumn<Client, String> clientCityCol;to TableColumn<Client, Address> clientCityCol;, otherwise it will not work.

0
source

All Articles