Vaadin Table not showing components

I have trouble with vaadin 6 table:

@Configurable(preConstruction=true)
public class Stock implements WindowComponent {
    
    @Autowired
    StockService service;
    
    WareSystemsDisplay display;

    public Stock(WareSystemsDisplay wareSystemsDisplay) {
        display = wareSystemsDisplay;
    }
    
    private Table categorySelector;
    private IndexedContainer icCat = new IndexedContainer();
    private Table stockTable;

    @SuppressWarnings("serial")
    @Override
    public AbstractComponent create(AbstractComponent parent) {
        
        GridLayout mainLayout = new GridLayout();
        mainLayout.setMargin(false);
        mainLayout.setWidth("99%");
        mainLayout.setHeight("99%");
        mainLayout.setColumns(6);
        mainLayout.setColumnExpandRatio(0, 5f);
        mainLayout.setColumnExpandRatio(1, 10f);
        mainLayout.setColumnExpandRatio(2, 5f);
        mainLayout.setColumnExpandRatio(3, 10f);
        mainLayout.setColumnExpandRatio(4, 5f);
        mainLayout.setColumnExpandRatio(5, 200f);
        mainLayout.setSpacing(true);
        
        for (int i=0; i<10; i++)
            mainLayout.insertRow(i);
        
        categorySelector = new Table();
        categorySelector.addStyleName("components-inside1");
        categorySelector.setSizeFull();
        categorySelector.setImmediate(true);
        categorySelector.setEnabled(true);
        categorySelector.setSelectable(true);
        categorySelector.setMultiSelect(false);
        icCat.addContainerProperty("Category name", Label.class, null);
        categorySelector.setContainerDataSource(icCat);
        categorySelector.addListener(new ValueChangeListener() {
            
                public void valueChange(ValueChangeEvent event) {
                    loadCategory((Long) ((AbstractComponent)event.getProperty().getValue()).getData());
            }
            
        });
        mainLayout.addComponent(new Label("Category:"), 0, 0, 4, 0);
        mainLayout.addComponent(categorySelector, 0, 1, 4, 7);
        
        Button addCategoryButton    = new Button("Add category");
        Button deleteCategoryButton = new Button("Delete category");
        
        mainLayout.addComponent(addCategoryButton, 1, 8, 1, 8);
        mainLayout.addComponent(deleteCategoryButton, 3, 8, 3, 8);

        Button addItemButton    = new Button("Add Item");
        mainLayout.addComponent(addItemButton, 1, 9, 3, 9);
        
        stockTable = new Table();
        stockTable.setSizeFull();
        stockTable.setSelectable(false);
        stockTable.setImmediate(true);
        
        mainLayout.addComponent(new Label("Stock:"), 5, 0, 5, 0);
        mainLayout.addComponent(stockTable, 5, 1, 5, 9);
        
        return mainLayout;
    }

    @Override
    public String getWComponentName() {
        return "Stock";
    }
    
    private ThreadLocal<StockCategory> selectedCategory = new ThreadLocal<StockCategory>();
    

    protected void loadCategory(Long id) {
        selectedCategory.set(service.find(id));
    }

    @Override
    public void refresh() {
        stockTable.removeAllItems();
        categorySelector.removeAllItems();
        
        List<StockCategory> cids = service.getAllCategories();
        for (StockCategory c : cids){
            Label l = new Label(c.getName());
            l.setData(c.getId());
            icCat.addItem(l);
        }
        
    }

}

I add label with text into container but table is empty, with clickable empty space. I do not know what to do at this point…

Okay, I managed to fix it by adding this line at then end. Can anyone tell me why is it necessary?

icCat.getContainerProperty(l, CATEGORY_NAME).setValue(l);

Hi,

on line 97 of your code you’re adding a new Item to the container, using the label l as the item ID. This does not automatically assign any properties so the single property in your container will have the default value (null).

Also, you might want to consider using just String for the property if you don’t need the label for any specific reason. It will be a bit faster to render. You could use the “c.getId()” as the item ID, so no need to set that as data of the label either.

Yes, I will do it for this one, but stock table will be full blow vertical layout so I had to figure it out. Strange that it does not do it automatically.

Hi,

the reason is that the parameter you give in your addItem call will just identify the item itself. The properties must be set separately using their own identifiers (or set automatically by e.g. BeanItemContainer).

You can read more about the property-item-container model here:
https://vaadin.com/book/-/page/datamodel.html#datamodel.overview

Thanks a lot!