简体   繁体   中英

How to enable a button when filling a textfield with VAADIN

I work with Vaadin. I have a text field and a button. My button is initially disabled. When my text field is filled with valid data my button must activate. I can not activate my button. Could you help me ? Thank you

public static DynTextField createFromElement(Element elt, DynForm form) {
    if (elt.getNodeName().equals("param") && elt.getAttribute("type").equals("TEXT")) {
        DynTextField dtf = new DynTextField();
        dtf.setForm(form);
        if (elt.hasAttribute("texte"))
            dtf.setCaption(elt.getAttribute("texte"));
        dtf.nom = elt.getAttribute("nom");
        if (elt.hasAttribute("FORMAT"))
            dtf.setFormat(elt.getAttribute("FORMAT"));
        dtf.setDescription(elt.getAttribute("description"));
        dtf.setStyleName("param" + (elt.hasAttribute("class") ? elt.getAttribute("class") : ""));
        return dtf;
    } else
        return null;
}

private void setFormat(String attribute) {
    binder = new Binder<>();
    binder.forField(this).withValidator(new RegexpValidator("Saisie obligatoire !!", attribute)).asRequired("Format Erroné").bind(No.getter(), No.setter());
    //new Binder<>().forField(this).withValidator(new RegexpValidator(attribute, "Format Erroné")).asRequired();

}

// convenience empty getter and setter implementation for better readability 
public static class No { 
 public static <SOURCE, TARGET> ValueProvider<SOURCE, TARGET> getter() { 
  return source -> null; 
 } 

 public static <BEAN, FIELDVALUE> Setter<BEAN, FIELDVALUE> setter() { 
  return (bean, fieldValue) -> { 
   //no op 
  }; 
 } 
}   

The program that creates my button. This is where I would like to make my button active.

public DynButton(DynForm form, String as400PGMName, String[] parameterList) {
    super(VaadinIcons.CHECK);
    this.as400PGMName = as400PGMName;
    if (parameterList.length == 1 && parameterList[0].equals(""))
        this.parameterList = new String[] {};
    else
        this.parameterList = parameterList;
    this.form = form;
    addClickListener(event -> {
        fireClickEvent(event);
    });

    addClickListener(this);
    impl = new DynComponentImpl();

     //boutton initially disable
     this.setEnabled(isActif());


}

You can do it with a listener on either the text field or the binder

textField.addValueChangeListener(e -> 
        myButton.setEnabled(!e.getValue().equals("")));

or

binder.addStatusChangeListener(e -> 
        myButton.setEnabled(!e.hasValidationErrors()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM