How to display database table contents with Table object

Dear all,

I’m trying to display the contents of a database table in a page using the Table object. I have no issues connecting to the database and am able to list one of the table fields with a Select object. I’m using the code below, I get no errors but no Table is shown in the page. With the debugger I’ve been able to verify that the query is sucessful and the users list contains the correct items when it is passed to the BeanItemContainer.

Any hint on why this code doesn’t work would be very helpful. Thank you.

        contentView = new Table();
        contentView.setWidth("100%");
        contentView.setHeight(400);
        contentView.setImmediate(true);
        contentView.setSortDisabled(false);
        contentView.setNullSelectionAllowed(true);
        contentView.setTableFieldFactory(new UserTableFieldsFactory());
        contentView.setEditable(true);
        contentView.setSelectable(true);
        contentView.setMultiSelect(true);

        contentView.setColumnHeader("companyname", "Compagnie");
        contentView.setColumnHeader("email",    "E-mail");
        contentView.setColumnHeader("function",    "Fonction");
        
        contentView.setVisibleColumns(new Object[] {
                "companyname",
                "email",
                "function"
        });
        contentView.setColumnHeaders(new String[] {
                "Compagnie",
                "E-mail",
                "Fonction"
        });

        tabP.addComponent(contentView);      
        
        loadUsers();


    protected final void loadUsers(){
    
        Query q = PM.getInstance().createQuery("select u from " + User.class.getSimpleName() + " u");
        List<User> users = q.getResultList();
        userContainer = new BeanItemContainer(User.class, users);
        contentView.setContainerDataSource(userContainer);
    }

So far your code looks ok, although (as it is not syntactically correct) I cannot say that for sure. Have you tried to check via debug-mode and analyze layouts wether there are layout-problems that might lead the table to have zero height / width?

You can’t name or modify properties before the actually exist. Move the naming and visibility of the columns down to the bottom of loadUsers()

Thank you all for the swift replies.

As Jens explained, I was loading the data at the wrong time, it has to be there already when the column headers and data columns are set.

Regards.

Ok, just one other thing, now I get the data on the Table object, but all of it, all columns in the data base are displayed. The instructions on the column headers work properly, but not the setVisibleColumns(). Any ideas why?

Thank you once more.

You should call setVisibleColumns() and setColumnHeaders() after setting the data source.
This is because different data sources can have different properties available, so setting the data source resets the visible columns.

If using Vaadin 7, it is sometimes more efficient to call table.setContainerDataSource(container, visibleColumns) rather than making those two calls separately.

Hi Henri,

I’m already setting column headers and the visible columns after loading the data, that’s why the table wasn’t showing up. I’m using Vaadin 6.6.

Thank you,

Luís