Numbers in TextField appear to be with format point.

When I put to TextField Integer/int values and then enter big value, number format point appears. For example: 8888 transfers to 8,888.

I use this field for entering port values. How to make TextFields display numbers correctly?

What Vaadin version are you using? are you sure that you don’t use Datasource or Converters?

I use Vaadin 7 beta10. I create this field via FieldGroup. And the value that is passed to the field is represented by int.

The textfield only processes Strings, so at some point you have to convert the value; are you perhaps calling the following code: tf.setConverter(Integer.class); .
By default Vaadin uses the StringToIntegerConverter class, which uses NumberFormat.getIntegerInstance(locale) with the locale specified for the UI (or if not set, Locale.getDefault()). To change the behaviour, you need to implement your own converter.

It is strange because I don’t use any converters, so by default StringToIntegerConverter should be used.

Here are some parts of code:


Field port;
FieldGroup uiBinder = new FieldGroup(new ServerBeanItem(new Server)); //ServerBeanItem is class that extends BeanItem<Server> and Server is class describing server fields like port (it is represented by int)

port = uiBinder.buildAndBind("Port: ", "port");

FormLayout content = new FormLayout();
content.addComponent(port);

Panel form = new Panel("Add Server");
form.setContent(content);

So as you can see, I don’t apply any converter.

StringToIntegerConverter is used, and uses the locale, which is why some users may see a dot or a comma or a small non-breaking space or some other separator there.

To prevent the use of a separator, you can use something like

StringToIntegerConverter plainIntegerConverter = new StringToIntegerConverter() {
    protected java.text.NumberFormat getFormat(Locale locale) {
        NumberFormat format = super.getFormat(locale);
        format.setGroupingUsed(false);
        return format;
    };
};
// either set for the field or in your field factory for multiple fields
tf.setConverter(plainIntegerConverter);

It appeared at the end SOOOO simple. Thank you a lot, you saved my nerves, I didn’t even guess that the source of this problem was in locale.

But another problem appeared when I read port number. It appears again with the separator and also with exception: Could not convert value to Integer.

If I correctly understood convertToModel should also be overridden.

It should also use the same formatter returned by getFormat(locale).

It looks like you have a value in the field that has been converted with one converter or locale and then try to parse it with another, or something like that - if you use the no-separator formatter, you should not see a message like “Could not convert ‘8,888’ to java.lang.Integer”.

Maybe a simple “?restartApplication” URL parameter would help if you made the change while the application was running or if your application server serializes application state (and thus the wrong string) over server restarts.

Well, I walked through the field composition and found out that my “8888” value is converted to “8,888” by FieldGroup.buildAndBind() when it reads value from data source. If I create empty field (adding new entry instead of editing existing one) converter works fine. And when FieldGroup reads data and receives “8888”, it then converts “8888” to “8,888”.

Hm, it is harder to explain than it seemed.


updated

It started to work when I created my own Factory extending default one and overridden findConverter method changing logic for fields with Integer values. And than set this factory to VaadinSession.

Bravvo Henri, this really works.

Thanks for this straightforward solution. :slight_smile:

it would be really helpfull to have NumberField addon as built-in UI component :frowning:


plainIntegerConverter:
this helped me a lot!
Tks

I have the same format point problem with combo box.
In my case I have a combo box where user have to select year of birth. Depending the keyboard and maybe a locale the values are presented example 1 990 or 1,990 (please see the attachment).
Can anyone say what I have to change or use to prevent mentined behaviour in the combo box?
22910.png

You can use setItemCaption for the presentation like this.
final int currentYear = Calendar.getInstance().get(Calendar.YEAR);
for (int i = 1900; i < (currentYear + 1); i++)
{
this.year.addItem(i);
this.year.setItemCaption(i, String.valueOf(i));
}

Thank’s, that helped!

Hi All,

I have a requirement like this my textfield will accept only numbers max length is 10. So if i used StringToIntegerconverter it will append the commas example 1,234,567,890 but if i want to edit the existing record it wont allow u to change because the max length is 10 commas are already appended due to that it was not changed . So please help me to how to resolve this problem.

How to achieve this kind of requirement.

Hi guys, any idea please, how the above solution would look in Vaadin 8.5.2? TextField does not have the setConverter method anymore…