how to set table column name

I looked around in the forum and i still don’t understand how to set a certain column name:

setColumnHeader(propertyId, header)

i understood that header is the new name i want, but
how do i get the propertyId of the column that i want to change name from?

I looked in these threads below, but i still didn’t understand that, and i don’t want to use setColumnHeaders(String headers) just to set one of them.


HERE


HERE

Thank you.

In our experience, we set all the visible columns and column headers using the one you don’t want to use because we typically are setting those properties from our container we want listed. If your container uses beans, it would be the bean property names (like name, if you have a getName() method; status, if you have a getStatus() method; etc.)

And if you are not using a BeanItemContainer, you should be using Container that holds Items, and Items are basically a set of Property objects (like a record or db row). The property id (in an Item) is generally what you’d think of as being the name of the field, again like “name” or “status”. It will match the name used when you populated the container, so most likely you’d have something like: setColumnHeader(“name”,“Name”);

Good luck…

I populated my container dropping my list of beans into it:

public class SumsPercentContainer extends BeanItemContainer<SumsPercent> {

	public static final Object[] NATURAL_COL_ORDER = new Object[]
 { "groupedSum",
			"percent" };

	public SumsPercentContainer(Collection<SumsPercent> collection)
			throws IllegalArgumentException {
		super(collection);
	}

}

Came back with an edit knowing how much David wuvs 'em :grin:
But talking alone i realised that the only method that returns what i need is getContainerPropertyIds()
so the solution is:

setColumnHeader(getContainerPropertyIds().iterator().next(), "adfgsgas");

… luckily the column i needed to rename is first, if it wasn’t, i guess a next() is needed for each index.

setColumnHeader(getContainerPropertyIds().iterator().next(), "adfgsgas");

Doesn’t work as espected, the ids are fetched in the alphabetical order of the filter names (haven’t tested), but when i added a third column, it was renamed, although it was the third, only explanation would be the alphabetical order.

I used setColumnHeaders instead.

It is not clear to me why you would want to set the header of a column without knowing what column it is (i.e. without knowing its ID).

Anyway, the order in which getContainerPropertyIds() returns the IDs is unspecified in Container - many container implementations currently do return them in the order they were added (e.g. IndexedContainer and BeanItemContainer), but do not specify this in their API documentation.

If you want to get the column order as it is shown on the screen (after any rearrangement the user might have done), you can also use Table.getVisibleColumns() - that might help before showing the table to the user and letting him rearrange it, or if you don’t allow the user to rearrange columns.

Certain, setColumnHeaders() is the most straightforward for beans where you know the columns (aka property) up front, and if you use setVisibleColumns() you can limit the columns to a subset if your bean has more properties than you’d like and you want to control their order.

But I tried setColumnHeader, and it worked as expected, giving it the field/property/column name as the property id, and the new label. The property ids for a bean items should be the fields/properties in your bean that can be retrieve with a ‘getXXX()’ method as you’d naturally expect.

That was my exact problem with setColumnHeader, how to know the column i aim to change name for.

Strange, i’m pretty sure i wrote it like: setColumnHeader(“groupedSum”, “Grouped Sum”); and didn’t work, could be that i haven’t noticed the space in “Grouped Sum”, because of capitalization. Works, thanks!

P.S. True, if last item added is first, then that must be the ordering, forgot for a moment that you can add indexed, while the rest beeing pushed ahead.

Thank you!

With the basic IndexedContainer you usually start with adding all the columns to the container by doing this:

container.addContainerProperty("name",String.class,"");
container.addContainerProperty("address",String.class,"");

The property Id’s are the first parameter you specify - in my example “name” and “address”. At this point you change the text that is in the table header with reffering to those objects (strings in this example but can be any object).

table.setColumnHeader("address","The persons address");

The only different to BeanItemContainer is that it automates the adding of container properties, and fills the items properties with the bean’s data. If your bean as for example a field “private String country;” the container will just do this behind the scenes:

container.addContainerProperty("country",String.class,null);
Item item = container.addItem(bean);
item.getItemProperty("country").setValue(bean.getCountry());

this means that you can modify the header with this code:

table.setColumnHeader("country","The country of choice");

When starting to use container it is good to know how they work on their very basic level. For this reason it is good to know how to use IndexedContainer inside out as it is quite simple compared to the rest of the containers (if something with the containers can actually be called simple…). All the other containers, like BeanItemContainer, is based on the same fundamental architecture.

Thank you. Good to not dig it myself up when the need comes. I always used BeanItemContainer thus far, to match my lists with Vaadin tables, only because of the automation.

I noticed Vaadin is built upon some consistent architecture i never noticed before anywhere. Not even the pattern crazed interviewers i had never mentioned or even asked anything like i found in Vaadin.

How to get names of Column Headers and how to get values of selected row id as an list or array

Would not @Named(“Column name”) work when using bean?