labatyo
labatyo

Reputation: 486

How to add specific color to Table row in Vaadin?

I'm trying to change the color of a row on a table based on a hex value for the row item. I'm basically trying to generate the css on the fly similar to generating a cssLayout like this

CssLayout content = new CssLayout( ) {
      @Override
      public String getCss( Component c ) {
        return "background: " + colorCode + ";";
      }
    };

Here's the code I'm using now

    table.setCellStyleGenerator(
    new Table.CellStyleGenerator( ) {
      public String getStyle( Object itemId, Object propertyId ) {
        return  "green";
      }
    } );

But it only works for setting the stylename, so I'd have to have millions of style names to accommodate all the possible hex values for the colors the user wants.

Upvotes: 4

Views: 5519

Answers (2)

Serge Farny
Serge Farny

Reputation: 932

You can use the CSSInject Add-on to add needed style name on the fly.

See https://vaadin.com/directory#addon/cssinject

String color = "#CCDDFF";
CSSInject css = new CSSInject(getUI());
css.setStyles("."+color+" { background-color: "+color+"; }");

Upvotes: 2

Sanjaya Liyanage
Sanjaya Liyanage

Reputation: 4746

In the existing CSS file you can define the color changes as background-color: #00ff00; for the style "green" and return the style green considering your constraints.Have a look color changes.

Upvotes: 0

Related Questions