get bean for table

I write the following


public BeanItemContainer<MyBean> getLista() {
		List<MyBean> lista = null;
		try {
			lista = this.getControl().getServiceBean().getListMyBean();
		} catch (ExcepcionDao e) {
			
			e.printStackTrace();
		}
		BeanItemContainer<MyBean> cr = new BeanItemContainer<MyBean>(lista);
		return cr;
	}

getListMyBean() is a list obtained from the database, MyBean is


@SuppressWarnings("serial")
public class MyBean implements Serializable {

	
	private String name;
	private int id;
	.............................get and set...................

and try to get the bean by clicking on the table,I write the following


@Override
	public void valueChange(ValueChangeEvent event) {
		Property property = event.getProperty();
	
		if (property == myTable) {
			Item item = myTable.getItem(myTable.getValue());
			MyBean myBean=(MyBean)item;
                        System.out.print(myBeng.getid.toString())	
				
			}
		}

	}

and get the following error

com.vaadin.data.util.BeanItem cannot be cast to MyBean
how to get the bean??

thanks in advance

Hi!

Instead of:


Item item = myTable.getItem(myTable.getValue());
MyBean myBean=(MyBean)item;

try:


BeanItem<MyBean> item = (BeanItem)myTable.getItem(myTable.getValue());
MyBean myBean=item.getBean();

cheers!

Ok gracias from Chile

Or, even better, try MyBean myBean = (MyBean)myTable.getValue();

getValue() gives the item id and BeanItemContainer uses the bean itself as a item id for the rows.

work fine

MyBean myBean = (MyBean)myTable.getValue();

work fine

BeanItemContainer container = (BeanItemContainer) myTable
.getContainerDataSource();
myBean = container.getItem(myTable.getValue()).getBean();

Gracias :lol: