Grid.addColumn(…) accepts also lambda expression as a parameter for providing the value to Grid. So if the conversion or formating is simple enough, that can be used.
myGrid.addColumn(MyBean::getAmount,
new NumberRenderer(new DecimalFormat("$ #.##")));
Result Grid
| ID | Name | Amount | |
| 00 | ABC | 1.98888 | $ 1.99 |
It seems to convert the column, but now i am getting an extra column with no heading, i want to update the existing “Amount” column.
Note: I am using setItems() to fetch fields from the bean and set them as column, in my application there are a lot of columns and only few require formatting so adding columns one by one is possible(but not ideal) but I wanted to know if there is any alternative.
Thank you, but it still does not solve my problem.
//this works perfectly but creates a new column
myGrid.addColumn(MyBean::getAmount,
new NumberRenderer(new DecimalFormat("$ #,##0.00")));
//I want to update the column instead of creating a new one but it throws me an error
myGrid.getColumn("amount").setRenderer(new NumberRenderer(new DecimalFormat("$ #,##0.00")));
error on Line 6:
The method setRenderer(Renderer<? super capture#6-of ?>) in the type Grid.Column<MyBean,capture#6-of ?> is not applicable for the arguments ()
Here is one gotcha of Vaadin 8 Grid with the bean-scan method of generating columns. When you call getColumn(“propertyName”), that won’t know the correct generic value type of the column, since it is only determined runtime. You should try reading the column to a separate variable of the correct generic type (this probably requires a typecast) and that will then make the setRenderer also use the correct implied type.
This is exactly what I was looking for! It worked like a charm, Thank you!
All I had to do was create a Column variable and typecast it before using setRenderer().