Jiri45
(Jiří Pavlovský)
October 29, 2015, 11:47am
1
Hello,
I’m using ListSelect to let user select a year number.
Problem is that the numbers get formatted with thousands separator, which I don’t want.
How can I get rid of it?
I tried to use a custom converter but it doesn’t seem to work.
My code is
package org.prosyn.Invoices.ui;
public class YearSelect extends ListSelect{
public YearSelect() {
setConverter(new YearToStringConverter());
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
for (int year = 1994; year <= currentYear; year++)
addItem( year);
setRows(3);
setConverter(new YearToStringConverter());
}
public static class YearToStringConverter implements Converter<Object, Integer> {
@Override
public Integer convertToModel(Object s, Class<? extends Integer> aClass, Locale locale) throws ConversionException {
if (s == null)
return null;
return Integer.parseInt((String) s);
}
@Override
public Object convertToPresentation(Integer integer, Class<?> aClass, Locale locale) throws ConversionException {
if (integer == null)
return null;
String formattedNumber = String.format("%d", integer);
return formattedNumber;
}
@Override
public Class<Integer> getModelType() {
return null;
}
@Override
public Class<Object> getPresentationType() {
return null;
}
}
}
Instead of
Converter<Object, Integer>
try something extending
StringToIntegerConverter
, and then setting
NumberFormat.setGroupingUsed
to false.
public class NoThousandsStringToIntegerConverter
extends StringToIntegerConverter
{
@Override
protected NumberFormat getFormat( Locale locale )
{
NumberFormat format = super.getFormat( locale );
format.setGroupingUsed( false );
return format;
}
}
Also in your original code, you are calling
setConverter(new YearToStringConverter())
; twice, which doesn’t seem right.
Jiri44
(Jiří Pavlovský)
October 31, 2015, 9:20am
3
Hi,
thanks for the reply. But how do I use StringToIntegerConverter with ListSelect?
setConverter(new NoThousandsStringToIntegerConverter())
won’t compile. Error is:
The method setConverter(Class<?>) in the type AbstractField is not applicable for the arguments (NoThousandsStringToIntegerConverter)
Ah, I see, the ListSelect expects the first generic type of the Converter to be Object rather than String, so out of the box
StringToIntegerConverter
won’t work. You’ll have to wrap it with something implementing Converter<Object, Integer>, something like this:
public class ObjectToIntegerConverter implements Converter<Object, Integer>
{
private final StringToIntegerConverter innerConverter = new StringToIntegerConverter()
{
@Override
protected NumberFormat getFormat( Locale locale )
{
NumberFormat format = super.getFormat( locale );
format.setGroupingUsed( false );
return format;
}
};
@Override
public Integer convertToModel(Object value,
Class<? extends Integer> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException
{
return innerConverter.convertToModel(value == null ? "0" : value.toString(), targetType, locale);
}
@Override
public Object convertToPresentation(Integer value,
Class<? extends Object> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException
{
return innerConverter.convertToPresentation(value, String.class, locale).toString();
}
@Override
public Class<Integer> getModelType()
{
return Integer.class;
}
@Override
public Class<Object> getPresentationType()
{
return Object.class;
}
}
Jiri44
(Jiří Pavlovský)
October 31, 2015, 6:43pm
5
This compiles. but the thousand separator (space) is still present.
See attached image. Moreover I don’t understand why the ListSelect always includes empty option as a first item.
Jiri44
(Jiří Pavlovský)
October 31, 2015, 7:38pm
6
I found out, that I can just set caption on the item and don’t have to use Converter at all.
Object itemID = addItem(i);
setItemCaption(i, new Integer(i).toString());
Still, I don’t understand why the ListSelect includes an empty option.
Jiri44
(Jiří Pavlovský)
October 31, 2015, 7:43pm
7
OK, found it. One has to use
setNullSelectionAllowed(false);