Why BeanItemContainer override Header I set?

The following is sample code:

          Table table = new Table();
	table.setSizeFull();
	table.setWidth("100%");
	table.setHeight("100%");
	table.setColumnReorderingAllowed(true);
	table.setColumnCollapsingAllowed(true);
	table.setImmediate(true);
	table.setSelectable(true);
	table.addContainerProperty("Name", String.class, "");
	table.addContainerProperty("Description", String.class, "");
	table.addContainerProperty("Remark", String.class, "");
	table.setVisibleColumns(new String[] {"Name","Description"});
	table.setColumnHeaders(new String[] {"field1", "fields2"});
	
	//IndexedContainer indexContainer = new IndexedContainer();
	
	List<ItemBean> list = new ArrayList<ItemBean>();
	
	for (int i = 0; i < 2; i++){
   		ItemBean bean = new ItemBean();
		bean.setName("Name"+i);
		bean.setDescription("Description"+i);
 		
		list.add(bean);
	}	
	BeanItemContainer<ItemBean> itemContainer = new BeanItemContainer<ItemBean>(list);
	table.setContainerDataSource(itemContainer);
	String[] columns = table.getColumnHeaders();
	
	System.out.println("Table Items:"+table.getContainerDataSource().size());
	for (int i = 0; i < columns.length; i++)
		System.out.println("Table Colunms:["+i+"]

"+columns[i]
);
}

My questions:

  1. If set BeanItemContainer as containersource will override I set header. don’t display field1 and field2. in UI will display Bean property

  2. I found colunm header will order by letter. for example, Description is always previours at Name.

When you first create a Table, it will use IndexedContainer internally by default. Any addContainerProperty() calls that you make are applied to that. When you bind the Table to another container, the previous settings made to the default container are lost.

Also note that binding a Table to a data source with setContainerDataSource() resets any visible column and column header settings (because the new container could have completely different columns), so you have to make those after binding.

Very thanks your reply. for my second question, I have three column, Name, Description, Remark. I want to according this order. but on browser, It will display as Description, Name, Remark. Do you know why?

My guess is that the column order is in the same order as the fields are defined in the bean.

You can set the order of the columns by using Table.setVisibleColumns() if I remember correctly.

It can’t still be resolved after I try your idea.

OK, now it work well. need to execute setvisibleColumn() at last.