Java – how to create a text field that only supports numbers in vaadin

I'm using vaadin text field. I want to limit it to numbers only I tried to override setValue () and return without calling Supervalue If the text is not a number, setvalue() is returned But there seems to be no job How can I correct it?

Solution

If I understand that your question is correct, you want to have a field that ignores all non numeric inputs and not only marks the field as invalid Vaadins architecture is designed so that every field in the browser is displayed on the server In my opinion, the cleanest way to achieve this is to have a browser field that allows you to enter letters and other wrong characters I can't find such a field in vaadin 7 There seems to be a vaadin 6 attachment called number field, but I didn't test it

>Connect this add-on to vaadin 7 or ask the author to do this > write your own domain Maybe extend vtextfield and textfieldconnector > perform all operations on the server side and accept latency and traffic (IMHO)

Because I don't think option 3 is the way to go, I probably shouldn't display this code, but it's the fastest way to implement this code

public class IntegerField extends TextField implements Textchangelistener {
String lastValue;

public IntegerField() {
    setImmediate(true);
    setTextChangeEventMode(TextChangeEventMode.EAGER);
    addTextchangelistener(this);
}

@Override
public void textChange(TextChangeEvent event) {
    String text = event.getText();
    try {
        new Integer(text);
        lastValue = text;
    } catch (NumberFormatException e) {
        setValue(lastValue);
    }
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>