Hi,
my problem is that don’t get it how to use the converter for Table to converte a Long to Date.
My Test.class has just one field called “created” with java type long (it contains a date value ofc)
BeanItemContainer<Test> container = new BeanItemContainer<License>(Test.class, list);
Table table = new Table();
table.setContainerDataSource(container);
table.setConverter("created", new StringToDateConverter());
What can I do to use that kind of “converter” but for LongToDate?? Not StringToDate.
Exception …
Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to java.util.Date
at com.vaadin.data.util.converter.StringToDateConverter.convertToPresentation(StringToDateConverter.java:37)
at com.vaadin.ui.CustomTable.formatPropertyValue(CustomTable.java:4127)
at com.vaadin.ui.CustomTable.getPropertyValue(CustomTable.java:4069)
at com.vaadin.ui.CustomTable.parseItemIdToCells(CustomTable.java:2348)
at com.vaadin.ui.CustomTable.getVisibleCellsNoCache(CustomTable.java:2187)
at com.vaadin.ui.CustomTable.refreshRenderedCells(CustomTable.java:1709)
... 78 more
Update: Added Samplecode
Working Code:
[code]
@Theme(“mytheme”)
public class MyUI extends UI {
@Override
protected void init(VaadinRequest vaadinRequest) {
BeanItemContainer<Test> container = new BeanItemContainer<Test>(Test.class,
Arrays.asList(new Test { new Test(1489097212000L), new Test(1489097212000L) }));
Table table = new Table();
table.setSizeFull();
table.setContainerDataSource(container);
// This DOES NOT work
// ----> table.setConverter("created", new StringToDateConverter());
setSizeFull();
setContent(table);
}
@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
}
}
[/code]
NOT WORKING WITH EXCEPTION!
[size=2]
[size=3]
@Theme("mytheme")
public class MyUI extends UI {
@Override
protected void init(VaadinRequest vaadinRequest) {
BeanItemContainer<Test> container = new BeanItemContainer<Test>(Test.class,
Arrays.asList(new Test { new Test(1489097212000L), new Test(1489097212000L) }));
Table table = new Table();
table.setSizeFull();
table.setContainerDataSource(container);
table.setConverter("created", new StringToDateConverter() {
@Override
public Date convertToModel(String value, Class<? extends Date> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
// I cannot use Long because super.conv...() doesn't take a long!
return super.convertToModel(value, targetType, locale);
}
@Override
public String convertToPresentation(Date value, Class<? extends String> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
// I cannot use Long because super.conv...() doesn't take a long!
return super.convertToPresentation(value, targetType, locale);
}
});
setSizeFull();
setContent(table);
}
@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
}
}
[/size]
[/size]
If I write my own LongToDateConverter class then I get the following error in my IDE Eclipse:
The method setConverter(Object, Converter<String,?>) in the type Table is not applicable for the arguments (String, LongToDateConverter)
Sample Class (ofc empty …)
public class LongToDateConverter implements Converter<Date, Long>{
@Override
public Long convertToModel(Date value, Class<? extends Long> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
return null;
}
@Override
public Date convertToPresentation(Long value, Class<? extends Date> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
return null;
}
@Override
public Class<Long> getModelType() {
return null;
}
@Override
public Class<Date> getPresentationType() {
return null;
}
}
So the table.setConverter(…) doesn’t want my Converter<Long,Date>
Whats wrong?
Thanks,
Max