Search code examples
javaxmlgwtuibinder

setting CSS style Uibinder Gwt


I want to change the color of a g:label using java code on a onBlur event. I am using eclipse, UIBinder.

This is what I have in mind although it doesn't work.

In my StandardDocumentDownload.ui.xml file

<ui:style>     
   .testStyle {
   }
   .styleRequiredData
   {
        color:red;

    }
 </ui:style>

this is the event in my standardDocumentDownload.java file

@UiHandler("comboTitle")
void onComboTitleBlur(BlurEvent event) {
    int title = comboTitle.getSelectedIndex();

    if(title == 0)
    {
        labTitleReq.setText("Please enter a value");
        labTitle.addStyleName("styleRequiredData");
    }
    else
    {
        labTitleReq.setText("");
    }

}

How could I add the color red to the existing style of the label upon the firing of the event.

Kind regards


Solution

  • See here under Programmatic access to inline Styles

    for you, it shoulbe be something like :

    <ui:style type="com.yourapp.YourClass.MyStyle">     
        .testStyle {
        }
        .styleRequiredData
        {
            color:red;
        }
    </ui:style>
    
    public class YourClass extends Widget {
        interface MyStyle extends CssResource {
            String testStyle();
            String styleRequiredData();
        }
    
        @UiField MyStyle style;
    
        /* ... */
    
        @UiHandler("comboTitle")
        void onComboTitleBlur(BlurEvent event) {
            int title = comboTitle.getSelectedIndex();
            if(title == 0){
                labTitleReq.setText("Please enter a value");
                labTitle.getElement().addClassName(style.styleRequiredData);
            } else {
                labTitleReq.setText("");
            }
        }
    }