Using BeanItem with ObjectProperty in a Grid

Hi,

I am using a Grid for displaying beans in a BeanItemContainer. Works just fine, but when beans have an ObjectProperty attribute the Grid will fails with exceptions similar to the following:

[code]
java.lang.IllegalArgumentException: Cannot remove converter, as renderer’s presentation type java.util.Date and column’s model com.vaadin.data.util.ObjectProperty type aren’t directly compatible with each other (in Column[propertyId:requestedAt]
)
at com.vaadin.ui.Grid$Column.setConverter(Grid.java:2313)
at com.vaadin.ui.Grid$Column.setRenderer(Grid.java:2259)
at com.vaadin.ui.Grid$Column.internalSetRenderer(Grid.java:2365)
at com.vaadin.ui.Grid$Column.setRenderer(Grid.java:2221)

[cut for brevity]

[/code]The
ObjectProperty concept
is also being used with that bean so I was wondering if I am doing something wrong here. Isn’t it possible to convert automatically via ObjectProperty#getValue() in a Grid? What am I doing wrong here?

Thanks

Henning

Can you provide some code? Anyway, using ObjectProperty inside a Java-Bean sounds pretty wrong to me…

Hi,

code example is rather straightforward:

import java.util.Arrays;
import java.util.Date;

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.data.util.ObjectProperty;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Grid;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

public class GridBeanWithObjectPropertyExample extends UI {
    
    @Override
    protected void init(VaadinRequest request) {
        Grid grid = new Grid(new BeanItemContainer<Bean>(Bean.class, Arrays.asList(new Bean())));
        grid.setSizeFull();

        VerticalLayout layout = new VerticalLayout();
        layout.setMargin(true);
        layout.setSpacing(true);
        layout.setExpandRatio(grid, 1);
        layout.addComponent(grid);
        
        setContent(layout);
    }

    private static class Bean {
        private ObjectProperty<Date> from;

        public Bean() {
            this.from = new ObjectProperty<>(new Date(), Date.class);
        }

        public ObjectProperty<Date> getFrom() {
            return from;
        }

        public void setFrom(ObjectProperty<Date> from) {
            this.from = from;
        }
    }

    @WebServlet(value = "/*", asyncSupported = true)
    @VaadinServletConfiguration(productionMode = false, ui = GridBeanWithObjectPropertyExample.class, widgetset = "org.vaadin.gridutil.demo.DemoWidgetSet")
    public static class Servlet extends VaadinServlet {
    }

}

However, the thing is, that I have to deal with some legacy bean code which have been used in a table. Those beans are used at different places. I have hoped that I can use these beans in a grid, while other forms and other tables (which shall not be replaced by a grid) are not affected.

You said that using ObjectProperties in a bean sounds pretty wrong to you … So what’s the way to go? Creating simple POJO beans and wrapping them into ObjectProperty (let’s say an OPJO :wink: beans when the data should be bound to UI components?

That looks really wrong to me as well, I don’t see the reason to wrap the bean’s fields in an ObjectProperty. …unless you are using it for some workaround in some very unusual situation, but I can’t imagine what such situation could be.

Bean is a private class, so BeanItemContainer can’t access it.

If you change the class to public (and add the grid to the layout before calling setExpandRatio) your code works. But still, there is no need to wrap from date inside an ObjectProperty.

In this case, the right way is to create simple POJOs and let BeanItemContainer wrap the collection of beans.

I suggest you should read the capters about containers/items/properties and binding data to ui components in the book of Vaadin!
It is well explained there and i could not tell you anything else.

If you still have some understanding issues after reading, i’ll gladly help you again then!