Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has something replaced BeanItemContainer in Vaadin 8?

I downloaded a copy of the vaadin-charts-video example project from GitHub (https://github.com/vaadin-miki/vaadin-charts-video) to get a feel for how Vaadin charts are supposed to be implemented. However that project, and most of the available demo code/projects I have found seem to be written using Vaadin 7 and prior coding conventions. I am jumping in with Vaadin 8.0.5 (and JDK8)...

These errors are appearing when running Maven:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (default-compile) on project intro-video: Compilation failure: Compilation failure:
[ERROR] /C:/Users/dev/vaadin/vaadin-charts-video/intro-video/src/main/java/org/vaadin/miki/charts/intro/IntroChartsUI.java:[19,28] cannot find symbol
[ERROR] symbol:   class BeanItemContainer
[ERROR] location: package com.vaadin.data.util
[ERROR] /C:/Users/dev/vaadin/vaadin-charts-video/intro-video/src/main/java/org/vaadin/miki/charts/intro/IntroChartsUI.java:[54,17] cannot find symbol
[ERROR] symbol:   class BeanItemContainer
[ERROR] location: class org.vaadin.miki.charts.intro.IntroChartsUI
[ERROR] /C:/Users/dev/vaadin/vaadin-charts-video/intro-video/src/main/java/org/vaadin/miki/charts/intro/IntroChartsUI.java:[54,62] cannot find symbol
[ERROR] symbol:   class BeanItemContainer
[ERROR] location: class org.vaadin.miki.charts.intro.IntroChartsUI

Can anyone tell me, has BeanItemContainer been replaced? And if so, by what?

For example, see Line 54 of https://github.com/vaadin-miki/vaadin-charts-video/blob/master/intro-video/src/main/java/org/vaadin/miki/charts/intro/IntroChartsUI.java BeanItemContainer<BookPrice> container = new BeanItemContainer<>(BookPrice.class, prices);

And if that doesn't appear to be the issue to more experienced eyes, please let me know what is...

like image 502
justinb Avatar asked May 23 '17 18:05

justinb


2 Answers

Container is dead

Vaadin 8 no longer has Container. The Vaadin data model has been largely simplified. Basically, there is no data model anymore. There are just data providers for the various elements. What you want to look into is DataProvider, as it largely replaces Container.

A short introduction to the changes is here: https://vaadin.com/blog/-/blogs/vaadin-framework-8-comes-with-java-8-native-apis

Generally, for most uses you will either use setItems directly or use a ListDataProvider.

Long live Container

To ease migrating your existing Vaadin 7 app to Vaadin 8, you can use the Vaadin 7 compatibility layer. This includes the old Vaadin data model and Container. Move to Vaadin 8 while making very few changes to your existing code base.

See:

  • One-minute video, Migrating to Vaadin 8
  • Vaadin.com documentation, Migration from Framework 7 to Framework 8.
  • Migrating to Vaadin 8 article by Thomas Kratz.
like image 180
Piotr Wilkin Avatar answered Nov 07 '22 04:11

Piotr Wilkin


As it has already been correctly said: Container is no longer.

You can directly set the items at the UI element like

grid.setItems(books);

This will create a ListDataProvider under the cover.

If you want to change the items (add/remove) to the UI element without setting the items anew then you can create the ListDataProvider yourself like this:

List<Book> data = new Arraylist<>(getBooks());
ListDataProvider<Book> dataProvider = new ListDataProvider<>(data);
Grid<Book> grid = new Grid<>();
grid.setDataProvider(dataProvider);

If you later want to work on the items you just work on the list

data.add(newBook);

and call

dataProvider.refreshAll();

or

dataProvider.refreshItem(book);

If the attributes of the items have changed you probably need to call

grid.markAsDirty();

so that the grid will fetch the new data from the items.

like image 41
Mihael Avatar answered Nov 07 '22 05:11

Mihael