Formatting Values in Grid Columns


I have a grid that reads values from a Bean Class.

private List<MyBean> results = new ArrayList<MyBean>(); private Grid<MyBean> myGrid = new Grid<>(MyBean.class); myGrid.setItems(results);

Sample Grid

| ID | Name | Amount |

| 00 | ABC | 1.09123|

I want to format the decimal values to round them off to 2 digits precision.

Modified Grid

| ID | Name | Amount |

| 00 | ABC | 1.10 |

How can I get the desired results?

Thanks in advance.

Maybe the parts Generating columns and Column renderers (close to bottom of page) from the docs could help you:


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.

That was my first guess. However, when i try to use mygrid.getColumn(“Amount”).setRenderer(…) it says

“The method setRenderer(Renderer<? super capture#13-of ?>) in the type Grid.Column<MyBean,capture#13-of ?> is not applicable for the arguments ()”

I am not able to fetch the value of the Grid itself.

Ok, so i 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.

I see, then your case is not very difficult, you should be able to use built in NumberRenderer

Just use it, you can give format specification in constructor parameter, in your case it is DecimalFormat

And here is example of format string to be used in your specific case

Thank you, but it still does not solve my problem.

//this works perfectly but creates a new column
                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 ()

What am I doing wrong??

Have you tried alternative method e.g.?

myGrid.getColumn(“amount”).setRenderer(MyBean::getAmount, new NumberRenderer(new DecimalFormat(“$ #,##0.00”)));

See more here:

And also refering to this issue:

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().

Thank you all for the suggestions!

hI sajid Sid,

I have the same problem but in different situation

Sample Grid

| state | Name |

| 00 | ABC |

I want to change the value of the state which 0 into a Available. how can I do this? How did u solved this problem… sorry I am just a beginner

Modified Grid

| state | Name |

| available | ABC |

Hi Adrian,

There is answer to your question in this thread: