How to show an object on a Grid column?

Hi guys, I was wondering how to show an Object on a Grid Column.

I have agrid and I am creating the columns this way:

	Grid<Reteica> grid = new Grid<>();
	grid.addColumn(Reteica::getId).setCaption("ID");
    grid.addColumn(Reteica::getCities).setCaption("Cities");
    grid.addColumn(Reteica::getState).setCaption("State");

But the problem is that the column Cities will be filled with all the cities that I have, but Cities is an object with more fields and is showing all the columns of Cities object in my Cities column with the toString method in the model, so I need to know the way to put in that column a specified field from cities like Reteica::getCities.getName() or something like that that will return an String but I need that the Grid kept the type Reteica because I will use selections and get the Reteica Object with Grid.asSingleSelection().getValue().

Any suggestion is valuable.

Cheers

There are various solutions to your case. If the number of Cities is small, then you can do simply string conversion as you already mention. In that case, instead of function reference you need to use lambda expression in your addColumn method. If the list is long, this may be bad UX. In that case you could leave Cities out of columns, and instead use details row, and list the cities in details row. This is so called master - detail pattern. Third alternative is that you do not use the bean Reteica directly, but create DTO object, where you have property for each city. This is workable approach if the list of cities is not long and number of cities is more or less the same per row.

Tatu Lund:
There are various solutions to your case. If the number of Cities is small, then you can do simply string conversion as you already mention. In that case, instead of function reference you need to use lambda expression in your addColumn method. If the list is long, this may be bad UX. In that case you could leave Cities out of columns, and instead use details row, and list the cities in details row. This is so called master - detail pattern. Third alternative is that you do not use the bean Reteica directly, but create DTO object, where you have property for each city. This is workable approach if the list of cities is not long and number of cities is more or less the same per row.

Thanks for your response
I think I could use the lambda expression, and the number of cities is small for shure, I was using lambda expresion for a comboBox with this:

comboBoxCities.setItemCaptionGenerator(p -> p.getName());
 comboBoxCities.setItems(Cities);

And that works fine for me, but in the grid I couldn’t find the CaptionGenerator to do the same for the Column, so if you can write an small example to do this I would really appreciate.

Cheers!

Was solved with lambda expressions, just remove the column and add a new one with the lambda expression

gridCity.addColumn(theCity -> {
					return theCity.getName();
				}).setCaption("State").setId("state");