In Vaadin 7.0, how to update JavaBean data supporting a table? Replace container? Replace Beans?

In Vaadin 7.0, when displaying JavaBean data in a Table with BeanContainer , what is the correct way to update a table with new data?

+4
source share
3 answers

The table controls the properties of table elements through Listeners. If you change the property of an element through an instance of a table element, the table will be notified and updated: for example.

container.getItem(beanInstance).getItemProperty("propertyName").setValue("newValue");

If, however, the bean changes outside the instance of the element, you need to report a table update. The easiest way (using the BeanContainer stock) is to simply remove, then add items.

Alternatively - and, I believe, preferably - you could create a BeanItemContainer extension that raises the ItemSetChange event, which will refresh the table.

 public class RefreshableBeanItemContainer<BEANTYPE> extends BeanItemContainer<BEANTYPE> { public RefreshableBeanItemContainer(Collection<? extends BEANTYPE> collection) throws IllegalArgumentException { super(collection); } public RefreshableBeanItemContainer(Class<? super BEANTYPE> type) throws IllegalArgumentException { super(type); } public RefreshableBeanItemContainer(Class<? super BEANTYPE> type, Collection<? extends BEANTYPE> collection) throws IllegalArgumentException { super(type, collection); } public void refreshItems(){ fireItemSetChange(); } } 
+8
source

It seems like one way to update JavaBean data is to replace the Table Container with another container. One side effect that I noticed is that some table parameters can be reset, for example, collapsed columns are no longer reset.

Another way is to save the container when replacing the contained Beans.

I am not sure that both of these approaches are considered correct. And I'm not sure what trade-offs may be for any of these approaches.

Here is an example application with a pair of tables and a button. When the user clicks the button, both tables receive new data. One table gets a new BeanContainer. Another table stores its BeanContainer, but loads the new beans.

Screen shot of Vaadin 7.0.4 app with a pair of Tables and a Button between them.

Just two classes in this application:

  • The main class of the application.
  • The JavaBean class used to store data.
 package com.example.replacebeansorcontainer; import java.util.ArrayList; import java.util.List; import java.util.UUID; import com.vaadin.data.util.BeanContainer; import com.vaadin.server.VaadinRequest; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Table; import com.vaadin.ui.UI; /** * Main UI class. * * ReplaceBeansOrContainerUI.java * * @author Basil Bourque * * Copyright © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so. * */ @SuppressWarnings( "serial" ) public class ReplaceBeansOrContainerUI extends UI { Table tableThatGetsFreshContainer; Table tableThatGetsFreshBeans; @Override protected void init( VaadinRequest request ) { // Create widgets: table on the left, button, table on the right. this.tableThatGetsFreshContainer = new Table( "Table That Gets Fresh Container", this.makeBeanContainer() ); this.tweakTable( this.tableThatGetsFreshContainer ); this.tableThatGetsFreshBeans = new Table( "Table That Gets Fresh Beans", this.makeBeanContainer() ); this.tweakTable( this.tableThatGetsFreshBeans ); Button reloadButton = new Button( "Reload Data" ); reloadButton.addClickListener( new Button.ClickListener() { @Override public void buttonClick( ClickEvent event ) { // Reload data in both tables when user clicks this button. System.out.println( "User clicked 'Reload Data' button. Replacing container (left) & beans (right)." + new java.util.Date() ); // One table gets a new Container. ReplaceBeansOrContainerUI.this.tableThatGetsFreshContainer.setContainerDataSource( ReplaceBeansOrContainerUI.this.makeBeanContainer() ); // The other table keeps its Container, but the Container beans are replaced. ReplaceBeansOrContainerUI.this.tableThatGetsFreshBeans.getContainerDataSource().removeAllItems(); // Cast the Container to BeanContainer to utilize the 'addAll' method. @SuppressWarnings( "unchecked" ) BeanContainer<UUID, MomentBean> beanContainer = (BeanContainer<UUID, MomentBean>)ReplaceBeansOrContainerUI.this.tableThatGetsFreshBeans.getContainerDataSource(); beanContainer.addAll( ReplaceBeansOrContainerUI.this.makeListOfBeans() ); } } ); // Compose Layout. final HorizontalLayout layout = new HorizontalLayout(); layout.setMargin( true ); layout.setSpacing( true ); layout.addComponent( this.tableThatGetsFreshContainer ); // Table on the left. layout.addComponent( reloadButton ); layout.addComponent( this.tableThatGetsFreshBeans ); // Table on the right. // Compose UI. this.setContent( layout ); } private void tweakTable( Table table ) { table.setSelectable( true ); } private List<MomentBean> makeListOfBeans() { List<MomentBean> beans = new ArrayList<MomentBean>(); for ( int i = 0; i < 20; i++ ) { beans.add( new MomentBean() ); } return beans; } private BeanContainer<UUID, MomentBean> makeBeanContainer() { // Instantiate empty container, with columns defined by my class' JavaBean fields. BeanContainer<UUID, MomentBean> container = new BeanContainer<UUID, MomentBean>( MomentBean.class ); try { // Indicate which field in Bean serves as the unique identifier. container.setBeanIdProperty( MomentBean.class.getDeclaredField( "uuid" ).getName() ); container.addAll( this.makeListOfBeans() ); // Add entire Collection of beans to container. } catch ( NoSuchFieldException e ) { // TODO Auto-generated catch block e.printStackTrace(); } catch ( SecurityException e ) { // TODO Auto-generated catch block e.printStackTrace(); } return container; } } 

JavaBean Class ...

 /** * */ package com.example.replacebeansorcontainer; import java.text.SimpleDateFormat; import java.util.UUID; /** * MomentBean.java * * @author Basil Bourque * * Copyright © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so. * */ public class MomentBean { // Bean fields. private final String clockTime; private final long nanoTime; private final UUID uuid; // Other member fields. private final SimpleDateFormat timeOnlyFormat = new SimpleDateFormat( "HH:mm:ss" ); /** * */ public MomentBean() { this.clockTime = this.timeOnlyFormat.format( new java.util.Date() ); this.nanoTime = System.nanoTime(); this.uuid = UUID.randomUUID(); } /** * @return the clockTime */ public String getClockTime() { return this.clockTime; } /** * @return the nanoTime */ public long getNanoTime() { return this.nanoTime; } /** * @return the uuid */ public UUID getUuid() { return this.uuid; } } 
+2
source

It seems that the solution is as simple as calling refreshRowCache() on the table - at least for me!

+2
source

Source: https://habr.com/ru/post/954884/


All Articles