How to change the titleStyle of a ButtonItem

I want to change the font color of a ButtonItem. I thought it would work by changing the titleStyle but it didn’t.

ButtonItem item = new ButtonItem("myButton);
item.setTitleStyle("styleOn");
item.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                 event.getItem().setTitleStyle("styleOff");
            }            
        });

...

.styleOn {
    color: green;	
}

.styleOnOver {
    color: aqua;   
}

.styleOff {
    color: red;
}

.styleOffOver {
    color: aqua;
}

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.

What do I need to change here?

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.

As far as I can see, a canvas does not have a setTitleStyle method.

I just want to change the color of the text which is presented on the Button of the ButtonItem. Changing its contents (its text) works fine through

event.getItem().setTitle(...)

but I also need to change the text color. Is there any way to do so?

A Button has setTitleStyle() and a ButtonItem’s Canvas is of type Button. See also setBaseStyle in the docs.

Button does not have setTitleStyle

Might be that I miss something here or that we’re talking about different things or versions. I also checked the latest svn revision of Smart GWT.

Regarding the documentation of setBaseStyle: I can’t see any helping doc when searching for setBaseStyle in http://www.smartclient.com/docs/6.5.1/a/b/c/go.html

This is my code in fragments:

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().

setBaseStyle() is present on all Button classes.

If you look at what getCanvas() returns you should find that it’s an instance of one of the Button classes - let us know if it doesn’t seem to be.

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:

public void onClick(com.smartgwt.client.widgets.form.fields.events.ClickEvent event) {
  ButtonItem item = (ButtonItem)event.getItem();
  Log.debug("Old style=" + item.getTitleStyle()); // logs 'styleOn'
  item.setTitleStyle("styleOff");
  Log.debug("New style=" + item.getTitleStyle()); // logs 'styleOff'
}

Might be that a redraw command or so needs to be invoked?

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.

ButtonItem.getCanvas() should return a Button. Please file and issue for this.

for now you could try the following as a temporary measure :

item.getCanvas().setProperty(‘titleStyle’, …);

Sanjiv

The suggestion works. I have filed issue
http://code.google.com/p/smartgwt/issues/detail?id=101
for this.
Thanks so far.