在Vaadin 7.0中,在JavaBean中使用Table显示BeanContainer数据时,使用新数据刷新表的正确方法是什么?
答案 0 :(得分:9)
该表通过Listeners监视表的Items的属性。如果您通过表格的Item实例更改项目的属性,则会通知并更新该表格:例如
container.getItem(beanInstance).getItemProperty("propertyName").setValue("newValue");
但是,如果bean在项目实例的外部更改,则需要告诉表刷新。最简单的方法(使用库存BeanContainer)只需删除然后添加项目。
或者 - 我建议,最好 - 你可以创建一个BeanItemContainer扩展来激发ItemSetChange事件,这将导致表刷新。
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();
}
}
答案 1 :(得分:2)
似乎刷新JavaBean数据的一种方法是将Table的Container替换为另一个Container。我注意到的一个副作用是可能会重置某些表的设置,例如折叠的列不再被折叠。
另一种方式似乎是在更换包含的Beans时保留Container。
我不确定这两种方法是否都被认为是正确的。而且我不确定这些方法可能会有什么权衡。
这是一个带有一对表和一个按钮的示例应用程序。当用户单击该按钮时,两个表都会获得新数据。一个表获得一个新的BeanContainer。另一个表保留了BeanContainer,但是加载了新的bean。
此应用中只有两个类:
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's 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类......
/**
*
*/
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 :(得分:2)
似乎解决方案就像在桌面上调用refreshRowCache()
一样简单 - 至少对我而言!