Problem with components inside the table which is using BeanItemContainer

Hello
I’m creating a component, which consists of the table. Table has two columns. Left column is a column of links displayed in TextFields and the second column is a column of buttons to preview this links in popup window. Table uses container, field factory for links and column generator for buttons column. The problem is that when I’m adding new items to the table some TextFields aren’t displayed (but I still can preview it with a button):

Setting up the table

MediaInfoGridContainer container = new MediaInfoGridContainer(list);
table.setContainerDataSource(container);
table.setTableFieldFactory(new UrlTextFieldFactory());
table.addGeneratedColumn("buttons", new PreviewButtonColumnGenerator());
table.setVisibleColumns(new String[] { "value", "buttons" });

MediaInfoGridContainer extends BeanItemContainer:

public class MediaInfoGridContainer extends BeanItemContainer<MediaInfoGridDto> {
		public MediaInfoGridContainer(List<String> list) {
			super(MediaInfoGridDto.class);
			//Init container
			for (String str : list) {
				addBean(new MediaInfoGridDto(str));
			}
		}

		@Override
		public BeanItem<MediaInfoGridDto> addItem()
				throws UnsupportedOperationException {
			MediaInfoGridDto dto = new MediaInfoGridDto();
			addBean(dto);
			return new BeanItem<MediaInfoGridComponent.MediaInfoGridDto>(dto);
		}
	}

MediaInfoGridDto is a bean:

public class MediaInfoGridDto implements Serializable {
		private String value;
		//Getters and setters are omitted

UrlTextFielFactory returns new TextField objects in its createField method and PreviewButtonColumnGenerator creates new Button in its generateCell method. Buttons have following listener to display links (button’s data field is it’s itemId):

previewButton.addListener(new Button.ClickListener() {
				private static final long serialVersionUID = 4580821711331959978L;

				@Override
				public void buttonClick(ClickEvent event) {
					MediaInfoGridDto gridItem = (MediaInfoGridDto) event.getButton()
						.getData();
					preview(previewFactory.getPreview(gridItem.getValue()));
				}
			});

New item is added to the table with

table.addItem();

I’m using Vaadin 6.6.2

The question is how can I add new row to this table

Thanks in advance

UPD: The problem may be related to the fact that the new elements have the same hash and it does not updates later by vaadin

Hi!

I have almost the same question. I have the table includes SQLContainer as a dataSource. Then I have a form where I insert new BeanItems to the table but adding is the problem?

Object id = staffTable.addItem(staffItem);
//staffTable.addItem(staffItem); This isn't working either
staffTable.setValue(id);
System.out.println(staffTable.getItemIds());
staffTable.requestRepaintAll();

From that println I could see that every time when I hit the commit button table have got new items, but I can’t see them, why is that?

EDIT: I tried that one too, no luck:


System.out.println("DataSource size1: " +staffTable.getContainerDataSource().size());// Zero like it should be because I am adding a first object to dataSource
//addingUserForm.commit();
staffTable.getContainerDataSource().addItem(staffItem);
System.out.println("DataSource size2: " +staffTable.getContainerDataSource().size()); ----------> Never seen this one?? WHY?

Thanks!
Sami

PLS help!

one month, no help at all…

I believe the issue of at least the first poster is indeed the hashcode. Read closely the following paragraph from the javadoc:

You are adding an empty MediaInfoGridDto each time which presumable has the same hashcode at insertion time.

This is a reason why I’ve avoided BeanItemContainers for some purposes.

Hi Sami,

First, BeanItems cannot be used with SQLContainer. SQLContainer only understands RowItems.

Second, your code:


Object id = staffTable.addItem(staffItem);

is backwards. Table.addItem(Object) takes an
item ID
and returns the item. Your code seems to assume that you pass in the item and get the id, which is not the case.

To add an item to the database using SQLContainer (or any container, really), you should first add an item to the container using addItem() (Note: no parameters). This call returns an itemId, which you can then pass to getItem(itemId). This is the item you should set at the item data source for the form. After editing you just commit the form and commit the SQLContainer and the new data is stored.

As for no responses, make sure to post in your own thread as you have a greater chance of someone noticing your question.

HTH,
/Jonatan

PS. it occurred to me that you might be building data POJOs from data that you read in from the database? If this is the case, you should really look into using some ORM implementation (such as EclipseLink or Hibernate) and a suitable container for these as they will allow working with POJOs much easier than SQLContainer.