Problems with Textfield Value send

Hello

I’am a newbie working with Vaadin. I have installed Vaadin 7.

I should like send the value from a textfield to another site textfield. I have created two sites with composite Design builder.

Entry Class :

public class Vaadin_test_v01aUI extends UI {
    
    Navigator navi;
    
    protected static final String MAINVIEW = "site1";    
    

    //main ma = new main();

    @WebServlet(value = "/*", asyncSupported = true)
    @VaadinServletConfiguration(productionMode = false, ui = Vaadin_test_v01aUI.class)
    public static class Servlet extends VaadinServlet {
    }
    
    @Override
    protected void init(VaadinRequest request) {
      
        getPage().setTitle("Navigation Example");
        
        // Create a navigator to control the views
        navi = new Navigator(this, this);
        
        navi.addView(MAINVIEW, new site1());
        navi.addView("", new main());            // Wird als Startseite festgelegt.
    }

    public void enter(ViewChangeEvent event) {
        // TODO Auto-generated method stub
        
    }
[/code]First Site : [code]
public class main extends CustomComponent implements View {
 
    private static final long serialVersionUID = 1L;

    Vaadin_test_v01aUI va = new Vaadin_test_v01aUI();
    myHelperClass helperClass = new myHelperClass();

    public main() {
        buildMainLayout();
        setCompositionRoot(mainLayout);

        // TODO add user code here
        
        button_1.addClickListener(new Button.ClickListener() {
            
            /**
             * 
             */
            private static final long serialVersionUID = 1L;

            @Override
            public void buttonClick(ClickEvent event) {
                // TODO Auto-generated method stub
                
                helperClass.setName(textField_1.getValue());   

                UI.getCurrent().getNavigator().navigateTo("site1");
            }
        });
   
        
    }


    @AutoGenerated
    private AbsoluteLayout buildMainLayout() {
        // common part: create layout
        mainLayout = new AbsoluteLayout();
        mainLayout.setImmediate(false);
        mainLayout.setWidth("100%");
        mainLayout.setHeight("100%");
        
        // top-level component properties
        setWidth("100.0%");
        setHeight("100.0%");
        
        // button_1
        button_1 = new Button();
        button_1.setCaption("Button");
        button_1.setImmediate(true);
        button_1.setWidth("-1px");
        button_1.setHeight("-1px");
        mainLayout.addComponent(button_1, "top:134.0px;left:60.0px;");
        
        // textField_1
        textField_1 = new TextField();
        textField_1.setCaption("My Textfield");
        textField_1.setImmediate(false);
        textField_1.setWidth("-1px");
        textField_1.setHeight("-1px");
        textField_1.setMaxLength(5);
        mainLayout.addComponent(textField_1, "top:60.0px;left:63.0px;");
        
        
        return mainLayout;
    }
    }


    
    
    
[/code]Second Page : [code]
public class site1 extends CustomComponent implements View {
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    
    public site1() {
        buildMainLayout();
        setCompositionRoot(mainLayout);
        
        textField_1.setValue(ma.helperClass.getName());
        
        
        
        button_1.addClickListener(new Button.ClickListener() {
            
            /**
             * 
             */
            private static final long serialVersionUID = 1L;

            public void buttonClick(ClickEvent event) {
                // TODO Auto-generated method stub
                
                UI.getCurrent().getNavigator().navigateTo("");
  
            }

            
        });

        //UI.getCurrent().getNavigator().navigateTo("main");
    }

    @Override
    public void enter(ViewChangeEvent event) {
        // TODO Auto-generated method stub

    }

    @AutoGenerated
    private AbsoluteLayout buildMainLayout() {
        // common part: create layout
        mainLayout = new AbsoluteLayout();
        mainLayout.setImmediate(false);
        mainLayout.setWidth("100%");
        mainLayout.setHeight("100%");
        
        // top-level component properties
        setWidth("100.0%");
        setHeight("100.0%");
        
        // button_1
        button_1 = new Button();
        button_1.setCaption("go to Site Main");
        button_1.setImmediate(true);
        button_1.setWidth("220px");
        button_1.setHeight("-1px");
        mainLayout.addComponent(button_1, "top:20.0px;left:20.0px;");
        
        // textField_1
        textField_1 = new TextField(ma.helperClass.getName());
        textField_1.setImmediate(false);
        textField_1.setWidth("-1px");
        textField_1.setHeight("-1px");
        mainLayout.addComponent(textField_1, "top:60.0px;left:20.0px;");
        
        return mainLayout;
    }
    
    

}

Helper Class :

package com.example.vaadin_test_v01a;

public class myHelperClass {
    
    String name;
     
    public String getName() {
        return name;
    }
    
    public void setName(String name1) {
        this.name = name1;
    }

}

Could anyone help me please?

Hello Michael,
you have two errors in your code.

  1. String is immutable. So you don’t update the String reference of myHelperClass but create a new String object
  2. The SecondPage’s TextField is not getting informed by the FirstPage. So the TextField can’t know that some values has been changed (object keeps the same, because String is immutable).

What you want is a Controller/Presenter/event bus getting informed by FirstPage’s ValueChangeListener informing SecondPage, so SecondPage can update the appropiate components.