Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

[TABLE & CONTAINER] Updated row disappear after adding a new one.

Mickaël Sandron
9 years ago Jun 18, 2012 3:58pm

Hello!

I have a strange bug who occurs with an updated row in a table.

This is my code:

final BeanItemContainer<FraisLibreSurPosteDTO> container = new BeanItemContainer<FraisLibreSurPosteDTO>(FraisLibreSurPosteDTO.class);
		table.setHeight("200px"); 		
		table.setContainerDataSource(container);
		List<Object> visibleColumnIds = new ArrayList<Object>();
		container.addNestedContainerProperty("montantUnitaire");
		container.addNestedContainerProperty("communication");
		visibleColumnIds.add("montantUnitaire");
		visibleColumnIds.add("communication");
		table.setVisibleColumns(visibleColumnIds.toArray());
		
		table.setEditable(true);
		addComponent(button);
		
		table.setTableFieldFactory(new TableFieldFactory() {
			
			@Override
			public Field createField(Container container, Object itemId,
					Object propertyId, Component uiContext) {
				if ("montantUnitaire".equals(propertyId)) {
					return new TextField();
				} else {
					return null;
				}
			}
		});
		button.addListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				FraisLibreSurPosteDTO dto = new FraisLibreSurPosteDTO();
				dto.setCommunication("COUNT" + container.getItemIds().size());
				dto.setMontantUnitaire(new BigDecimal(container.getItemIds().size()));
				table.addItem(dto);
			}
		});
		

So I have a table in editable mode. I also have a button who adds a new row into my table.
1 click on the button => 1 new row (screenshot 1)
No problem with that...

But if you change the value of "montantUnitaire" by for example 78 (screenshot 2) and after that you click on the button, you will see that the row with montantUnitaire = 78 is not visible!
In fact, the row is still there but the input box or the value is not visible. (screenshot 3)

I'm using the last Vaadin version (6.8.0) but I also had this problem with 6.7.6 version.

Could you help me? :bashful:

Last updated on Jun, 18th 2012
Pan Zupa
9 years ago Dec 22, 2012 4:59pm
Eric Schripsema
9 years ago Jan 10, 2013 9:22pm
Henri Sara
9 years ago Jan 11, 2013 8:28am
Damian Czernous
8 years ago Feb 23, 2014 1:09am

This is because FraisLibreSurPosteDTO (JavaBean) implements hashcode & equals methods.

Vaadin BeanItemContainer uses JavaBean as an ID that is used within hashed collections such as HashMap to associate rows with beans.

From Java we know that HashMap works fine with immutable value beans, but JavaBean is not immutable, but mutable by definition. Vaadin BeanItemContainer expects to work with JavaBean and adds additional restriction.

Form API doc:
"BeanItemContainer uses the beans themselves as identifiers. The Object.hashCode() of a bean is used when storing and looking up beans so it must not change during the lifetime"

Please, remove hashcode method from FraisLibreSurPosteDTO & all should work fine. However this is not a solution, but just an exercise to be done.

If you need hashcode & equals to compare by value and hashcode is based on standard algorithm that relays on bean's values then use BeanContainer (not BeanItemContainer) instead.

Felix M
7 years ago Jan 20, 2015 12:44pm
Deleted User
7 years ago Feb 13, 2015 2:16pm