Table .setVisibleColumns doesn't appear to work

Hi, first of all I’d like to say I’ve been working with Vaadin for a little while now and I think its a pleasure to use as a web UI platform.

One issue I have found is that table.setVisibleColumns seems not to work as expected:

final Table table = new Table("my table");
		
		table.setContainerDataSource(getTableDataContainer());
		table.setVisibleColumns(new Object[] {RESOURCE_ID_COLUMN, 
											  NAME_DESC_COLUMN, 
											  CLASS_COLUMN, 
											  PERCENTAGE_COLUMN});

where getTableDataContainer() is:

	private Table getTableDataContainer() {
		Table table = new Table();
		table.addContainerProperty(RESOURCE_ID_COLUMN, String.class,  null);
		table.addContainerProperty(NAME_DESC_COLUMN, String.class,  null);
		table.addContainerProperty(CLASS_COLUMN, Boolean.class,  null);
		table.addContainerProperty(PERCENTAGE_COLUMN, Double.class,  null);
		table.addContainerProperty(RESOURCE_OBJECT_COLUMN, Part.class,  null);
		return table;
	}

and on a button click I’m doing:


	public void addResourceToTable(Part part, Double percentage) {
		resourceTable.addItem(new Object[] { part.getId(), 
											 part.getDescription(), 
											 part.isPartClass(), 
											 percentage, 
											 part }, resourceTable.size() );
	}

The column names like NAME_DESC_COLUMN are just strings set elsewhere.

As you can probably see all I’m trying to achieve with setVisibleColumns is to hide the object column where I’m stashing my data object because otherwise it just displays an ugly toString() on the thing.

If I remove the .setVisibleColumns the table works fine but obviously the object column is displayed.

You didn’t mention what is the problem, what does the table look like?

I hope you don’t add a GeneratedColumn after the method call or something like that.

The way how you use another table as a data source for your table worries me a bit. I think it should work, although I don’t see the reason to use a table and not some pure container type.

Ah sorry yes the problem occurs when I try to add an item including the object column, it doesn’t display and therefore doesn’t appear to be working.

I mainly just use a Table as the container due to the way I was previously adding items to the table (I was using hard coded test data, I’m now getting it from elsewhere), and table seemed like a simple convenient way to construct some test data.

I tried using just the one table without a data source and setting the column headers that way, but it doesn’t seem to work either.

So what was exactly the problem? The new row doesn’t appear? Is there an exception in the console?

The problem is probably this: From the javadocs for addItem(Object cells, Object itemId):[quote]
Adds the new row to table and fill the visible cells (except generated columns) with given values.
[/quote]

Keyword, visible cells. That means that you probably can’t give the value for the hidden column with that method. Remove it from the array and it should work.

I usually use the addItem(object) to add items and it should work in your case too if you want to specify the value for the hidden column:

Item item = resourceTable.addItem(part);
item.getContainerProperty(RESOURCE_ID_COLUMN).setValue(part.getId());
item.getContainerProperty(NAME_DESC_COLUMN).setValue(part.getDescription());
item.getContainerProperty(CLASS_COLUMN).setValue(part.isPartClass());
item.getContainerProperty(PERCENTAGE_COLUMN).setValue(percentage);
item.getContainerProperty(RESOURCE_OBJECT_COLUMN).setValue(part);

I’ve here user the object part as the row item id instead of a integer as you had. Integers works just as well, but I think that the object is more handy, because you can then call resourceTable.getValue(); to get the select object directly.

Ok. I don’t see a particular problem there, except that you are adding the items to a different table than the one which you created.

Ah that does the trick - the row now appears, thank you.

I think therefore like you say, you cannot use:

addItem(Object cells, Object itemId)

In combination with setVisibleColumns because it will only work for visible cells.