Dynamic changing of the field type in a Table - setContainerProperty()

I have a table and it is populated from a database.
And I have table field called ‘myprop’.

In my table class I do this:
for (Object ii: table.getItemIds()) {
Item kk = table.getItem(ii);
kk.getItemProperty(“myprop”).setValue(new Checkbox(null, bb));
}

Instead of checkbox the following text is appearing in the column ‘myprop’
java.awt.Checkbox[checkbox0,0,0,0x0,invalid,state=false]

Question is how can I change dynamically the property type of ‘myprop’ to ‘CheckBox.class’ ?

I can do table.getContainerProperty(kk, “myprop”) but there is no set function like
table.setContainerProperty(“myprop”, CheckBox.class);
I do see that there is
addContainerProperty(“tradeinmake”, CheckBox.class, null);

I may be missing something… there may be simple solution and above a dumb qn…

Hi,

You cannot change the type of a container property but you can add a property, which type is Component.class:

table.addContainerProperty("myprop", Component.class, null);

and now you can set for example Label and CheckBox components to this property.

-Henri

Additionally you should check your imports… seems to be that you’ve imported java.awt.Checkbox instead of com.vaadin.ui.Checkbox.

HTH, Maik

I tried, but now it throws unsupported exception at ototrader.ui.QuoteList.(QuoteList.java:22), which has this

line 22: table.addContainerProperty(“myprop”, Component.class, null);

The table is already bound to a container from sql database table.

The exception stacktrace is like this:

com.vaadin.event.ListenerMethod$MethodException
Cause: java.lang.UnsupportedOperationException
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:507)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
at com.vaadin.ui.AbstractComponent.fireEvent(AbstractComponent.java:1107)
at com.vaadin.ui.TabSheet.fireSelectedTabChange(TabSheet.java:631)
at com.vaadin.ui.TabSheet.setSelectedTab(TabSheet.java:418)
at com.vaadin.ui.TabSheet.changeVariables(TabSheet.java:441)
at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariables(AbstractCommunicationManager.java:1060)
at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleUidlRequest(AbstractCommunicationManager.java:561)
at com.vaadin.terminal.gwt.server.CommunicationManager.handleUidlRequest(CommunicationManager.java:260)
at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:438)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.UnsupportedOperationException
at com.vaadin.data.util.BeanItemContainer.addContainerProperty(BeanItemContainer.java:256)
at com.vaadin.ui.AbstractSelect.addContainerProperty(AbstractSelect.java:736)
at com.vaadin.ui.Table.addContainerProperty(Table.java:2639)
at ototrader.ui.QuoteList.(QuoteList.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:487)
… 22 more

BeanItemContainer (which you are using) gets its property types from the bean (via BeanItem), and does not allow changing then afterwards. Also, I believe it is not possible to override how BeanItemContainer creates BeanItems in the current version.

To use a component in a BeanItemContainer, you need to have accessors for the component in your bean. Alternatively, you could use some other container.

If you want to show a Checkbox instead of the value, you could overwrite getPropertyValue() from the Table. Or, you could hide the actual column and use a
generated column
.