Java – Vaadin: Dynamically change a textfield’s background color

javavaadin

How does one dynamically change a textfield's visual properties, say background color?

An example might be during a validation to modify the background color dependent on the value entered.

Best Solution

In your war (this will be inside WebContent if you are using ecilpse) have a file in this path VAADIN/themes/customrunno/styles.css (asuming you are using runno as your theme)

With this content:

@import url(../runo/styles.css);

.my_bg_style {
     background: #99EE6B;  
}

In your init method:

setTheme("customruno"); // same as the folder folder under VAADIN/themes

change your textfield background:

textField.addStyleName("my_bg_style"); // same as in styles.css

to remove the style just do:

textField.removeStyleName("my_bg_style");