List<Person> displayed in a Table


For example I have a Java Bean named Person with tho attributes : Surname and Name (the both are a String type).

I read all the records in the database with the Hibernate ORM and I obtain a List as answer.

In the book of Vaadin it’s indicated to populate the Vaadin Table with a code as : table.addItem(new Object { “Hi”, “There” }, “item ID 1”);

But I must do a loop on the List for fill the Table and not convert each Person object in an Object for using the code : table.addItem(new Object
{ “Surname”, “Name” }, null);

How can I easyly display all the records in the Table?

How to do this?

For advance, thank you.

See you soon.


Read up on the Viritin addon and use that. Check the
example code of the project on GitHub
. Everything you’ve asked is solved there

table.setContainerDataSource(new BeanItemContainer(Person.class, persons);

8.5. Collecting Items in Containers
- 8.5.4. BeanContainer

Thank for this soluce.

I found this :

for (int i = 0; i < listeGamme.size(); i++)
Gamme gamme = listeGamme.get(i);

            Item item = table.addItem(i);

[/code]But when I use your code whose seems better :

table.setContainerDataSource(new BeanItemContainer(Gamme.class, listeGamme)); … all the attributes are displayed and I just want to display 2 columns (not all attributes of the Gamme Object).

See you soon.

Christmas, :wink:


you can use table.setVisibleColumns(Object …) to set the columns (property ids) you want to display in the table.

If Gamme is a POJO and has get and set methods and let’s say it has name, firstname, title, address, email attributes then you set

table.setVisibleColumns(new Object[] { "firstname", "name", "title" });

to only show these 3 columns. The order also defines the display order in the table.

You can then use table.setColumnHeaders(String …) to set the header texts for the colums. From what I see in the code your language would be french, thus:

table.setColumnHeaders("Prénom", "Nom", "Titre"); would do the trick.

Pay attention that the container datasource has to be defined BEFORE you set the visible columns and that the number of entries in the visible column and in the column headers has to match.




Sorry I didn’t see these lines of code in the Vaadin documentation.

Thank for your help.

It’s cool.

Bye, :wink: