generateCell() for generated column called repeatedly?

I am adding a checkbox to a table using Table.addGeneratedColumn(), like this:

                  specimenTable.addGeneratedColumn("selected", new Table.ColumnGenerator() {			
			@Override
			public Component generateCell(Table source, Object itemId, Object columnId) {		
				CheckBox c = new CheckBox();
				selectedCheckBoxes.put(c,itemId);
				return c;
			}
		});
	     specimenTable.setVisibleColumns(new Object[] {"selected", "specno", "lname", "fname", "MR", "site", "surgdate", "signdate", "att", "diagnosis"});

This appears to work; that is, the checkbox shows up in the UI of the table where it should. However, the HashMap selectedCheckBoxes contains multiple checkboxes for the same table row (itemId), even though only one checkbox is visible, and on investigation generateCell() is called more than once (typically 4) for the same table row (itemID).

I have another table where I’ve used the same construct, and it works; that is, generateCell() is called only once for each row. One difference between the two is that in the one that works the table is created in the constructor for a CustomComponent, whereas in the one that doesn’t work the table is constructed in a Property.ValueChangeListener for another table (when a row in the patients table is selected, the specimen table is created). However, I’ve verified that the specimen table creation code itself runs only once; its only generateCell() that is called repeatedly, apparently by some sort of interrnal UI creation routine (Table.refreshRenderedCells()).

Any enlightenment in this area would be greatly appreciated.

Matthew Fleming, MD
Fleming Dermatopathology

Hi,

The generateCell is called each time the Table needs to build its visible content. You could either add the table to its parent after it has been configured or add to cell generator as late as possible. And you could return the instantiated checkbox from your hash map if your are caching it anyways.

cheers,
matti

Thanks – immensely helpful. This appears to be the difference between the two scenarios (where my code works and where it doesn’t): in the first case the table the generated column is added to the table before the table is added to the layout, and generateCell() is called only once; in the second case the generated column is added to the table after the table is added to the layout, and generateCell() is called more than once. As a solution I’ll probably do what you recommend and just cache the checkbox.

Best regards,

Matthew