Table component

Hi all!

Code:


Table table = new Table();
table.setSelectable(true);
...
addComponent(table);
table.select(new Integer(1));

Works correctly. After opening the window, selects the first row in the table

But code:


table.addListener(new ItemSetChangeListener() {

            private static final long serialVersionUID = -9008670032927481120L;

            @Override
            public void containerItemSetChange(ItemSetChangeEvent event) {
                if (event.getContainer().size() == 0) {
                    selectedItem = 0;
                    updateButtonLayoutUp.recordsExist(false);
                } else {
                    updateButtonLayoutUp.recordsExist(true);
                }
                table.select(new Integer(1));
            }
        });

Not selects first row in the table, after refresh data container.
Why is this happening?

Table.select() takes an item ID as a parameter, not an index. To select the first item try something like this:

table.select(table.getItemIds().iterator().next());

Hello Henri!

Unfortunately your code does not select the first row 8o

Any more ideas?

It depends whether you replace the container in the table, or add items to it. The ItemSetChangeEvent is only fired when items are added to or removed from the container.

This is understandable. Through ItemSetChangeEvent I know that the container has been updated. How do I choose the first entry in the table?

You might have some other problems in your code. This example seemed to work fine:

final Table table = new Table();
table.addContainerProperty("foo", String.class, null);
table.setSelectable(true);

table.addListener(new Container.ItemSetChangeListener() {

     public void containerItemSetChange(ItemSetChangeEvent event)
          table.select(table.firstItemId());
     }
});

Button addItemButton = new Button("Add item");
addItemButton.addListener(new Button.ClickListener() {

     public void buttonClick(ClickEvent event) {
          table.addItem(Math.random()).getItemProperty("foo").setValue("bar");
     }
});

Risto, thanks, that’s the “key” that solves my problem :grin: