Table : dynamic cell colors

I have a collection of items with associated colors and I want to change background color of current cell instead of just showing html color code. Is it possible?

A bit “kludgy” way of doing it would be to show Label-components in the table with each label in “HTML” mode. Then you could just add proper HTML in those labels with correct color codes set.

To do this in case where you have the colors in datasouce, just use Table.addGeneratedColumn(…)

Or, you could use the non-kludgy setCellStyleGenerator() which exists for this purpose. Or did I misunderstand?


        tbl.setCellStyleGenerator(new CellStyleGenerator() {

            public String getStyle(Object itemId, Object propertyId) {
                if (itemId==myId && propertyId==myProperty) {
                    // creates style ".i-table-cell-content-mystyle" for the TD
                    return "mystyle";
                }
            }

        });

Hope this helps!

Best Regards,
Marc

This is a perfect solution if you have defined the colors in css. If you are using arbitary colors, setCellStyleGenerator is not the solution.

I am :frowning:

No worries - the “kludgy” way works just fine. (Here kludgy == web 1.0 programming style)

Indeed it works! Thanks!

I m using the setCellStyleGenerator but it is giving
null
for propertyId. i am using vaadin 6.5.2.

table.setCellStyleGenerator(new Table.CellStyleGenerator() {

			@Override
			public String getStyle(Object itemId, Object propertyId) {

}}

The method is called once for the whole row style (with null propertyId) and once for every cell (with non-null propertyId). You can return null in the cases you don’t want to handle.

The javadoc somehow implies this but is not very clear. I added a comment about this to a javadoc ticket, to be improved.

yesssss… it’s working … thanks Henri for your help !!!

:):smiley: