Registration addValueChangeListener

Hi
Sorry for noob question

I have created a component implements HasValue, all is fine ,
I have create an event listener both on Lit and Server

i just can’t find the right to implement : Registration addValueChangeListener

my code

@Tag("country-picker")
@JsModule("./app/countries/country-picker.js")
public class CountryPicker extends Component implements HasValue<HasValue.ValueChangeEvent<ACountry>,ACountry>,HasLabel {

    private ACountry aCountry = null;
    /** this is EventListener interface **/
    private  CountryPickerValueChangeListener valueChangeListener;
    public CountryPicker() {

        new PickerValueChange(this,detail -> {
            if (detail != null) {
                aCountry = Countries.getInstance().getCountryByIso2(detail.getString("value"));
                if (valueChangeListener!=null) {
                    valueChangeListener.valueChange(aCountry);
                }

            }

        });



    }


    @Override
    public void setValue(ACountry aCountry) {
        getElement().setProperty("value",aCountry.getIso2());
    }


    @Override
    public ACountry getValue() {

        return aCountry;

    }

    public void valueChange(CountryPickerValueChangeListener valueChangeListener) {
        this.valueChangeListener = valueChangeListener;
    }

    @Override
    public Registration addValueChangeListener(ValueChangeListener<? super ValueChangeEvent<ACountry>> valueChangeListener) {



        return null;
    }

    @Override
    public void setReadOnly(boolean b) {

    }

    @Override
    public boolean isReadOnly() {
        return false;
    }

    @Override
    public void setRequiredIndicatorVisible(boolean b) {

    }

    @Override
    public boolean isRequiredIndicatorVisible() {
        return false;
    }

    public interface CountryPickerValueChangeListener {
        void valueChange(ACountry aCountry);
    }
}

by the way this is result :slight_smile:

I recommend you to use the AbstractSinglePropertyField for your usecase

There is an example here: vaadin-react-field/src/main/java/com/example/application/ui/lit/LitMonthYearField.java at main · jcgueriaud1/vaadin-react-field · GitHub

That will simplify your code.

If you want to implement it yourself then you can read how it has been done internally: flow/flow-server/src/main/java/com/vaadin/flow/component/internal/AbstractFieldSupport.java at main · vaadin/flow · GitHub

Sometimes it’s not nice to work with Generics :smiling_face_with_tear:

1 Like

Thanks, It’s worked.