Sorry if it's a silly question , but I'm just starting learning GWT and got stuck to this problem for a long time ... . Here is my code :
public void onModuleLoad() {
HelloWorldPanel helloWorldPanel = new HelloWorldPanel();
RootPanel.get().add(helloWorldPanel);
//RootPanel.getBodyElement().appendChild(helloWorldPanel.getElement());
Button b = new Button();
b.setText("button B");
b.addClickHandler(new ClickHandler()
{
@Override
public void onClick(ClickEvent arg0)
{
System.out.println("click B");
Window.alert("Hello button B");
}
});
RootPanel.get().add(b);
}
And this is my HelloWorldPanel.java :
public class HelloWorldPanel extends Composite
{
interface HelloWorldUiBinder extends UiBinder<Widget , HelloWorldPanel> {}
private static HelloWorldUiBinder uiBinder = GWT.create(HelloWorldUiBinder.class);
@UiField TextBox nameTextBox;
@UiField Button goButton;
public HelloWorldPanel()
{
initWidget(uiBinder.createAndBindUi(this));
nameTextBox = new TextBox();
nameTextBox.setText("World");
goButton = new Button("Go");
goButton.addClickHandler(new ClickHandler()
{
@Override
public void onClick(ClickEvent event)
{
System.out.println("clicked , event = " + event);
Window.alert("Hello " + nameTextBox.getValue());
}
});
}
}
And HelloWorldPanel.ui.xml :
<ui:UiBinder
xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui">
<g:HTMLPanel>
<g:TextBox ui:field="nameTextBox" />
<g:Button ui:field="goButton" text="Go"/>
</g:HTMLPanel>
</ui:UiBinder>
It's a very basic "Composite" widget , but the TextBox and Button in HelloWorldPanel just not working. The default value ("World") is not shown , and button has no reaction , no System.out message ...
But the Button defined in onModuleLoad() just works well , I don't know why.... Can somebody tell me where goes wrong ? Thanks a lot !
Another question :
What's the difference between RootPanel.get().add(helloWorldPanel);
and RootPanel.getBodyElement().appendChild(helloWorldPanel.getElement());
? It seems the same result ...