Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT ValueListBox, Renderer and ProvidesKey

Tags:

gwt

How to implement a GWT ValueListBox inside an Editor with a specific list of objects, my code:

...
@UiField(provided = true)
@Path("address.countryCode")
ValueListBox<Country> countries = new ValueListBox<Country>(
        new Renderer<Country>() {

            @Override
            public String render(Country object) {
                return object.getCountryName();
            }

            @Override
            public void render(Country object, Appendable appendable)
                    throws IOException {
                render(object);
            }
        },          
        new ProvidesKey<Country>() {
            @Override
            public Object getKey(Country item) {
                return item.getCountryCode();
            }

        });
...

The Country class

public class Country  {
    private String countryName;
    private String countryCode;
}

But, during the GWT compilation I'm getting this error:

Type mismatch: cannot convert from String to Country
like image 825
Christian Avatar asked Feb 01 '26 16:02

Christian


1 Answers

The problem is that you are trying to edit the address.countryCode (looking at the path annotation) with editor for Country. To make this work, you should change the path to address.country and do the assignment of the address.countryCode after editorDriver.flash(). Something like:

Address address = editorDriver.flush();
address.setCountryCode(address.getCountry().getCountryCode());

To support this, the Address class should have the Country object as property.

You may have assumed that the ValueListBox will work like classical select where the key is assigned to the property. Here the whole object is assigned. So in your case Country object can not be assigned to address.countryCode and vice-versa.

Btw. you can correct the renderer (like the code below) and take care of null objects as arguments in the Renderer and Key Provider.

new Renderer<Country>() {
...
            @Override
            public void render(Country object, Appendable appendable)
                    throws IOException {
                appendable.append(render(object));
            }
...
}
like image 155
Goran Nastov Avatar answered Feb 03 '26 06:02

Goran Nastov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!