Convert from Calendar (XMLGregorianCalendar) to Date

Hi all,

I’m a Vaadin newbie and I’d like to ask some questions about converting fields. I have a JavaBean with a Calendar (XMLGregorianCalendar) attribute. What I wanna do is trying to generate a form from this bean. I have create my own FieldFactory that implements FormFieldFactory. In this class I override the createField method. My code is as follows:

	public Field createField(Item item, Object propertyId, Component uiContext) {
		Class<?> type = item.getItemProperty(propertyId).getType();
		if (Date.class.isAssignableFrom(type) || Boolean.class.isAssignableFrom(type)
				|| String.class.isAssignableFrom(type) || Long.class.isAssignableFrom(type)) {
			Field field = DefaultFieldFactory.get().createField(item, propertyId, uiContext);
			if (String.class.isAssignableFrom(type)) {
				String valor = "Hardcoding" + (String) propertyId;
			return field;
		if (XMLGregorianCalendar.class.isAssignableFrom(type)) {
                        DateField df = new DateField();
			df.setValue(new java.util.Date());
			df.setCaption((String) propertyId);
                       return df;			
		else {
			Property property = item.getItemProperty(propertyId);
			String pid = (String) propertyId;
			Select select = new Select(pid);
			return select;

Instead of showing my Calendar attribute as a TextField (the default field that is generated by the DefaultFieldFactory) I want to create a new DateField component in my form. The problem erases when I add this particular property to my form in the Form.class:

        // Adds all the properties to this form
        for (final Iterator<?> i = propertyIds.iterator(); i.hasNext();) {
            final Object id =;
            final Property property = itemDatasource.getItemProperty(id);
            if (id != null && property != null) {
                final Field f = fieldFactory.createField(itemDatasource, id,
                if (f != null) {
                    addField(id, f);

In line 10, I’ve already returned my DateField from MyFieldFactory but when I try to set property value to my DateField the following exception is printed:

2011-07-14 14:54:56.091:WARN::/vaadin/
java.lang.IllegalArgumentException: DateField only supports Date properties
	at com.vaadin.ui.DateField.setPropertyDataSource(
	at com.vaadin.ui.Form.setItemDataSource(
	at com.vaadin.ui.Form.setItemDataSource(
	at mju.ntj.agendaUnificada.vaadin.MyVaadinApplication.init(
	at com.vaadin.Application.start(
	at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.startApplication(
	at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(
	at javax.servlet.http.HttpServlet.service(
	at org.mortbay.jetty.servlet.ServletHolder.handle(
	at org.mortbay.jetty.servlet.ServletHandler.handle(
	at org.mortbay.jetty.servlet.SessionHandler.handle(
	at org.mortbay.jetty.handler.ContextHandler.handle(
	at org.mortbay.jetty.webapp.WebAppContext.handle(
	at org.mortbay.jetty.handler.ContextHandlerCollection.handle(
	at org.mortbay.jetty.handler.HandlerCollection.handle(
	at org.mortbay.jetty.handler.HandlerWrapper.handle(
	at org.mortbay.jetty.Server.handle(
	at org.mortbay.jetty.HttpConnection.handleRequest(
	at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(
	at org.mortbay.jetty.HttpParser.parseNext(
	at org.mortbay.jetty.HttpParser.parseAvailable(
	at org.mortbay.jetty.HttpConnection.handle(
	at org.mortbay.thread.QueuedThreadPool$

    public void setPropertyDataSource(Property newDataSource) {
        if (newDataSource == null
                || Date.class.isAssignableFrom(newDataSource.getType())) {
        } else {
            throw new IllegalArgumentException(
                    "DateField only supports Date properties");

I’ve tried to figure how solve this out. I have read that it’s possible to convert one to another using a WrapperField. Unfortunately, I couldn’t make it works :frowning: . The funny thing is that if I try to convert my Calendar attribute to a TextField or another UI Component there is no problem. Can somebody explain me how I could fix this issue, please?.

Thnx in advance,

, what exactly are you passing to
? Can you show me that code? I don’t see any calls to
in the code you have pasted here.

You cannot set the field value in the field factory - it is automatically set later by form.setItemDataSource().

See e.g.
this example
on FieldWrapper. Your FormFieldFactory can then call the default factory to create the field to wrap (or create it explicitly) and return it wrapped in a FieldWrapper.