Focus on Table cell

I have this problem: I have a button in a Panel for adding a new bean on an editable Table contained in the Panel. I know how to scroll to the last row, but I am not able to give the focus to a cell (for example the first) of the last row, the one just added. I tried to cache the field in the class that uses the TableFieldFactory and, recalling it on button click, call field.focus(), but it does not work. How can I do?

Hi,


Edit: Ok, a bit verbose example… Shortly in words: store itemId when adding new item, then focus desired field for that item in the FieldFactory (which will be called later). Remember to reset so that you don’t keep focusing it again and again.

This seems to do it:

package com.example.atest;

import com.vaadin.Application;
import com.vaadin.data.Container;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.Table;
import com.vaadin.ui.TableFieldFactory;
import com.vaadin.ui.Window;

public class AtestApplication extends Application {

    private final Table tbl = new Table("Beans");
    private final BeanItemContainer bic = new BeanItemContainer<MyBean>(
            MyBean.class);

    private MyBean focusBean = null; // new bean to be focused

    @Override
    public void init() {
        final Window main = new Window("Atest Application");
        setMainWindow(main);

        // pre-add som beans
        for (int i = 0; i < 3; i++) {
            MyBean b = new MyBean();
            b.setName("Bean" + i);
            bic.addBean(b);
        }

        tbl.setContainerDataSource(bic);
        tbl.setEditable(true);
        tbl.setPageLength(5);

        tbl.setTableFieldFactory(new TableFieldFactory() {
            public Field createField(Container container, Object itemId,
                    Object propertyId, Component uiContext) {

                Field f = DefaultFieldFactory.get().createField(container,
                        itemId, propertyId, uiContext);

                // if a new bean was added, focus its "name" field
                if (itemId == focusBean && "name".equals(propertyId)) {
                    f.focus();
                    focusBean = null;
                }

                return f;
            }
        });

        main.addComponent(new Button("Add", new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                focusBean = new MyBean(); // mark this to be focused
                bic.addBean(focusBean);
                if (tbl.size() > tbl.getPageLength()) {
                    tbl.setCurrentPageFirstItemIndex(tbl.size());
                }
            }
        }));

        main.addComponent(tbl);

    }

    public static class MyBean {

        private String name = "";
        private int age = 0;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

    }
}

One odd thing I noticed: setCurrentPageFirstItemIndex() can easily screw this up - hence the if(). Not sure why yet, I’m getting more coffee now!

Best Regards,
Marc

Thank you. It was a good help.