In the new Vaadin 7.4 release, the new Grid
widget debuted as an alternative to the venerable Table
.
After getting a Grid displayed, I later want to replace the entire set of data with fresh data. Rather than update the individual rows, I want to simply replace them.
I happen to be using a BeanItemContainer
for easy read-only display of some objects with JavaBeans-style getter methods.
I considered two approaches:
BeanItem
objects withContainer::removeAllItems
method.BeanItem
objects with theBeanItemContainer::addAll
method.BeanItemContainer
.Grid::setContainerDataSource
and pass a new instance ofBeanItemContainer
constructed with fresh data.Below is a sample application (Vaadin 7.4.2) showing both approaches. A pair of identical Grid widgets appear. Each has a button that updates data with either approach.
Results
The first approach (removing items and adding items) works. The fresh data immediately appears.
The second approach (replacing container rather than items) seems like it should work, with nothing contrary suggested in the scant documentation. But nothing happens. No exceptions or errors occur, yet no fresh data appears. I opened Ticket # 17268 on Vaadin trac for this issue.
Perhaps there are other better ways. Please post or comment with any alternatives.
Example App
Three classes are displayed below. You should be able to copy-paste into a new Vaadin 7.4.x app.
I use Java 8 Lambda syntax and the new java.time classes. So you may need to change your project's settings to use Java 8. In NetBeans 8 that means
Project > Properties > Sources > Source/Binary Format (popup menu) > 1.8
.MyUI.java
Get your Vaadin app going.
AstronomersLayout.java
The main part of the example.
Astronomer.java
The data, the bean items, stored in a BeanItemContainer for display in a Grid.
A nested Enum provides a safer way to refer to the field names in the other class,
AstronomersLayout
for call tosetColumnOrder
.You can simply get the record you have removed from button through clickListener with
.getSelectedRow()
. After this you can remove your item from your grid with.removeItem()
.IE:
Bye!