Search code examples
gwtmethodswidgetcalluibinder

uibinder gwt method call


I'm trying to get used to GWT and UiBinder at the moment. But I can't solve this problem. An example to show you what I mean:

MainMenu.ui.xml

<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:gwt="urn:import:com.google.gwt.user.client.ui" xmlns:my="urn:import:com.wn.webapp.client.UiBinder">

   <gwt:VerticalPanel>
     <my:TopMenu/>
     <gwt:VerticalPanel>
         <gwt:HTMLPanel>
             <gwt:TextBox/>
         </gwt:HTMLPanel>
         <my:ItemList/>
         <my:PageMenu/>
     </gwt:VerticalPanel>
   </gwt:VerticalPanel>
</ui:UiBinder

I created a MainMenu and embedded some ui.xml files into it. This works fine. The website looks good.

But how can I do this? This is the code for my PageMenu.ui.xml file, which I embedded into MainMenu.ui.xml .

public class PageMenu extends Composite{

  private static PageMenuUiBinder uiBinder = GWT.create(PageMenuUiBinder.class);

  interface PageMenuUiBinder extends UiBinderWidget, PageMenu{}

  public PageMenu(){
      initWidget(uiBinder.createAndBindUi(this));
  }

  public void setButtonText(ArrayListString textIds){
       //doessomething
  }
}

Now I want to call for ex. the setButtonText() method in onModuleLoad().

public void onModuleLoad()
{
    MainMenu mainmenu = new MainMenu();
    RootPanel.get().add(this.mainmenu);
    // call it here (setButtonText())
}

How can I do this?

Greetings Laura (I'm not such an experienced programmer yet. So pls be aware of that, when you try to answer :D) THX


Solution

  • To get access to the button.setText() you must have access to the button first. So, your PageMenu.ui.xml must have something like:

    <gwt:Button ui:field="button" />
    

    and your PageMenu.java must have the field declaration:

    @UiField
    Button button;
    

    Implement the getters for the PageMenu (at MainMenu) and for the button (at PageMenu), then you can do:

    public void onModuleLoad()
    {
        MainMenu mainmenu = new MainMenu();
        RootPanel.get().add(this.mainmenu);
        mainmenu.getPageMenu().getButton().setText("What you want.");
    }