Data Sharing between Panels

Hi all,

I’m new to Vaadin and I’m stucked at a little design problem:

I have three classes (no internals), let’s call them Main, PanelA and Panel B.
In the Main class, PanelA and PanelB are created and placed. PanelA contains a text field and a button, PanelB only labels.
If the button in PanelA is clicked, the label in PanelB should shown the value of the textfield entered in PanelA. (ClickListenerfor the Button in PanelA is implemented and works for showing an popup window)

Now how is the way to do this? Are there a kind of session to share, do i need to define DTOs in the main class and give to the panel constructors or … ?

Would be nice if somebody could give me hint to get more understanding of Vaadin design approaches :slight_smile:

Cheers,
Benny

Well, this can actually be done in several ways a bit depending on the usecase.

If you do not have many fields just have the text fields and labels share a property data source. Here is an example of that:


        ObjectProperty<String> sharedProperty = new ObjectProperty<String>("");

        final TextField field = new TextField();
        field.setPropertyDataSource(sharedProperty);

        Label label = new Label();
        label.setPropertyDataSource(sharedProperty);

        Button button = new Button("Commit", new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                field.commit();
            }
        });

If you have lots of fields then you most likely are talking about Forms and you want to create a shared DTO between two forms. Here is an example of that:


       BeanItem<MyDTO> sharedItem = new BeanItem<MyDTO>(new MyDTO());

        final Form formA = new Form();
        formA.setItemDataSource(sharedItem);

        final Form formB = new Form();
        formB.setItemDataSource(sharedItem);

        Button button = new Button("Commit", new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {            
                formA.commit();
            }
        });

The chapter in the book talking about this is
Chapter 9: Binding Components to Data

Hi John,

thanks for your answer. The hint with BeanItem lead me to the right solution. I already used it, but created a new one for every panel class, thought it would be enough to give them the same DTO in their constructors!

Cheers,
Benny