TreeTable and Data Binding

Hi!

I am new in Vaadin and try to use TreeTable. It is my code:


        TreeTable treeTable = new TreeTable("TreeTable", initializeTreeData());
        treeTable.setColumnHeader("Name", "1gh");

        treeTable.setWidth("300px");
        treeTable.setColumnWidth("", 50);
        treeTable.setColumnHeaderMode(Table.COLUMN_HEADER_MODE_HIDDEN);

        mainWnd.addComponent(treeTable);

    public Container initializeTreeData() {
        HierarchicalContainer data = new HierarchicalContainer();
        data.addContainerProperty(NAME, String.class, "");
        data.addItem(new NCObjectItem(ReferenceResolver.resolveID(new BigInteger("9130487750113864559"))));
        return data;
    }

    public class NCObjectItem extends PropertysetItem {

        NCObjectItem(NCObject object) {
            try {
                addItemProperty("Id", new ObjectProperty<BigInteger>(object.getID()));
                addItemProperty(NAME, new ObjectProperty<String>(object.getName()));
            } catch (RemoteException e) {
                log.error("Can't get parameters from object", e);
            }
        }
    }

In result I see table with one collapsed item without caption. I cheked that
object.getName()
returns not empty String.

Could you help me with it?

Please notice that the parameter for addItem() is an

item identifier

,
not
the item to be added. The addItem() creates the added item. [1]

As you are using HierarchicalContainer, you should add the properties to that, and when an item is created, it will have those properties. You’ll just need to set values for them, much like in the following example, but for the HierarchicalContainer, which you then bind to the table:

final Table table = new Table("The Brightest Stars");
        
// Define two columns for the built-in container
table.addContainerProperty("Name", String.class, null);
table.addContainerProperty("Mag",  Float.class, null);

// Add a row the hard way
Object newItemId = table.addItem();
Item row1 = table.getItem(newItemId);
row1.getItemProperty("Name").setValue("Sirius");
row1.getItemProperty("Mag").setValue(-1.46);


[1]
Notice, however, that Table uses the toString() of the item ID by default for the item caption, and you can sometimes use that feature so that the actual item isn’t used at all.

Big thanks! It’s work.

But it is not so clear from API or documentation (book and javadocs). Could you say me the true way for solve this case. I want set model for tree table that will take there data from my (not POJO) objects? I thought that it should be resolved with extending from HierarchicalContainer. If it true could you write some code for example?

Thanks for you help!

See
this example
. Well, it’s for POJOs.

I didn’t quite get what your objects are if not POJOs?

HierarchicalContainer can not be bound to any POJOs…or other objects… (easily at least).

this code can not work for me in vaddin 7. It seems the container read the -1.46 as double. Could you pls help me work this out?
java.lang.IllegalArgumentException: Value is of invalid type, got java.lang.Double but java.lang.Float was expected
at com.vaadin.data.util.IndexedContainer$IndexedContainerProperty.setValue(IndexedContainer.java:954)
at com.netease.dts.dashboard.view.dashboard.DashboardView.(DashboardView.java:60)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at com.vaadin.navigator.Navigator$ClassBasedViewProvider.getView(Navigator.java:341)
at com.netease.dts.dashboard.DashboardNavigator$2.getView(DashboardNavigator.java:99)
at com.vaadin.navigator.Navigator.navigateTo(Navigator.java:554)
at com.netease.dts.dashboard.DashboardNavigator.navigateTo(DashboardNavigator.java:49)
at com.netease.dts.dashboard.DashboardUI.updateContent(DashboardUI.java:76)
at com.netease.dts.dashboard.DashboardUI.init(DashboardUI.java:57)
at com.vaadin.ui.UI.doInit(UI.java:698)
at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:214)
at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:74)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1422)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:379)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:845)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1689)
at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:225)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1676)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:581)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1174)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:511)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1106)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:213)
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:119)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134)
at org.eclipse.jetty.server.Server.handle(Server.java:524)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:319)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:253)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:273)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93)
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.executeProduceConsume(ExecuteProduceConsume.java:303)
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:148)
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:136)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:671)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:589)
at java.lang.Thread.run(Thread.java:745)

That example is mighty old, some Java version defaults has changed since then. Try this one:

row1.getItemProperty(“Mag”).setValue(-1.46f);