Customizing TextField ?

I am trying customize a TextField and I don’t understand how I can get the words when the user is typing ?
Example, if user typing word “abc” I should get this words one by one.
I’m reading the Vaadin7 Book but I don’t know how to do this ?

how can I do ?

thanks

Hi,

if you want the input in real-time, use a TextChangeListener (see
https://vaadin.com/api/com/vaadin/event/FieldEvents.TextChangeListener.html
). Note that you also need to set the text change mode you want (see the rest of the API).

wow, thanks for your attention.

I resolved the problem using StringBuffer, while the user typing on keyboard I get the text and format how I need.


public class CpfField extends TextField implements TextChangeListener{    
    private final StringBuffer CPF = new StringBuffer();    
    
    public CpfField(){
        super("CPF");
        setImmediate(true);
        setMaxLength(14);
        addTextChangeListener(this);
    }

    @Override
    public void textChange(TextChangeEvent event) {
        if(!event.getText().trim().isEmpty()){
            if(event.getText().length() == 3){
                CPF.append(event.getText());                    
                CPF.insert(3,".");
            }else if(event.getText().length() == 7){
                CPF.setLength(0);
                CPF.append(event.getText());
                CPF.insert(7,".");
            }else if(event.getText().length() == 11){
                CPF.setLength(0);
                CPF.append(event.getText());
                CPF.insert(11,"-");
            }else{
                CPF.setLength(0);
                CPF.append(event.getText());
            }
        }else{
            CPF.setLength(0);
            setValue("");
        }
        
        setValue(CPF.toString());
    }
}

Thanks !

fernando paiva:
wow, thanks for your attention.

I resolved the problem using StringBuffer, while the user typing on keyboard I get the text and format how I need.

public class CpfField extends TextField implements TextChangeListener{    
    private final StringBuffer CPF = new StringBuffer();    
    
    public CpfField(){
        super("CPF");
        setImmediate(true);
        setMaxLength(14);
        addTextChangeListener(this);
    }

    @Override
    public void textChange(TextChangeEvent event) {
        if(!event.getText().trim().isEmpty()){
            if(event.getText().length() == 3){
                CPF.append(event.getText());                    
                CPF.insert(3,".");
            }else if(event.getText().length() == 7){
                CPF.setLength(0);
                CPF.append(event.getText());
                CPF.insert(7,".");
            }else if(event.getText().length() == 11){
                CPF.setLength(0);
                CPF.append(event.getText());
                CPF.insert(11,"-");
            }else{
                CPF.setLength(0);
                CPF.append(event.getText());
            }
        }else{
            CPF.setLength(0);
            setValue("");
        }
        
        setValue(CPF.toString());
    }
}

Thanks !

Fernando, esse código hoje não funciona mais, e eu estou precisando muito dessa formatação…você teria condições de me ajudar hoje? Vaddin 14 estou usando