The “styleOn” works, the button text is green and I can see the color change to aqua when I’m moving over the button. When clicking on the button I change the title style to “styleOff”, but that does not have any consequences, even though a later getTitleStyle shows that the style name had been changed as expected.
Naming collision: FormItems have a titleStyle which applies to the text that labels the form item. You probably want to be calling getItem().getCanvas().setTitleStyle() if your intent is to the change the title of the button dynamically.
com.smartgwt.client.widgets.form.fields.ButtonItem item = new ButtonItem("name");
item.addClickHandler(...);
...
public void onClick(com.smartgwt.client.widgets.form.fields.events.ClickEvent event) {
// ok: event.getItem() is instance of ButtonItem
// not ok: ((ButtonItem)event.getItem()).getCanvas() is instance of Canvas, not of Button
}
The canvas of a ButtonItem, at least what I get returned, is of type Canvas, not of type Button. The only canvas with a setTitleStyle seems to be StretchImgButton which is not returned from getItem().
In my case, getCanvas() on the ButtonItem does not return a Button, only a Canvas. This can also be seen in the Eclipse variables inspection view when checking the members of the ButtonItem. So, as expected, casting the returned canvas to a Button leads to a ClassCastException. I also checked this against the compiled mode: Also a ClassCastException presented in the SmartClient DevConsole.
public void onClick(com.smartgwt.client.widgets.form.fields.events.ClickEvent event) {
ButtonItem item = (ButtonItem)event.getItem();
// this leads to ClassCastException
Button button = (Button)item.getCanvas();
}
Basically setting the title style on a button item after creation works well:
com.smartgwt.client.widgets.form.fields.ButtonItem item = new ButtonItem("name");
item.setTitleStyle("styleOn");
With the code above I am able to style the text on my (click) button as I want. The styles ‘styleOnOver’, ‘styleOnFocused’, etc. are used as documented. Fine.
Dynamically changing the style, e.g. in a click handler, does not work, so the following code changes exactly the style property which was working before, but without any effect:
You need to call APIs on the underlying Button, not the ButtonItem. If you look at the actual class of the object being returned by getCanvas(), I think you’ll find it’s a type of Button (there are a few) which supports either setTitleStyle or setBaseStyle.