focus problem on layout which setvisible to false

Hi all, it took my all day to identify this problem. I do have a form with a text field wrapped inside a panel. What i want to achieve is when i click a button the panel will show up (now shown when first generated) and my text field will have the focus. But it throw exception when i first load the application. And after a day i found out the “focus” is the culprit. I"m using vaadin 7 nightly 20120706. Hope someone give me some advice how to work around this.


 TextField tf = new TextField("test");
 tf.focus();

        FormLayout formLayout = new FormLayout();
        formLayout.addComponent(tf);

        Panel detailPanel = new Panel("user");
        detailPanel.addComponent(formLayout);   
        detailPanel.setVisible(false);

        addComponent(detailPanel);

I think that you can’t focus something which is invisible.

I would try to make the Textfield global and try to focus() after setVisible(true). Something like that:

private TextField form_tf = new TextField("test");
private Panel detailPanel = new Panel("user");

private void initInvisbleForm() {

        FormLayout formLayout = new FormLayout();
        formLayout.addComponent(form_tf);

        detailPanel.addComponent(formLayout);   
        detailPanel.setVisible(false);

        addComponent(detailPanel);

}

private void theButton() {
        Button bt_focus= new Button("Test");
        bt_focus.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(Button.ClickEvent event) {
                detailPanel.setVisible(true);
                form_tf.focus();
            }
        });
}

(Just code example.)

In Vaadin 7, no information about a component that is invisible is sent to the browser - it is as if the component didn’t exist so it cannot have focus etc.

Many thanks for the solution! It does help!