Wednesday, June 23, 2010

Re: Is this the correct way to add an Widget directly to DOM ?

Your question seems a little confusing, but your idea is not quite
right. Firstly you shouldn't have to call super.onAttach(), the real
problem is you are effectively bypassing GWT's widget/event system by
trying to get the widget's element and attach it directly. In effect
you're mixing GWT and Javascipt and it's not being done correctly.

By adding to the DOM I think you mean add to the <body> element,
rather than adding to a particular element.

You give the example:
RootPanel.get("myId").add(testWidget );

... but if you want to add to the body, just do:
RootPanel.get().add(testWidget );

... and get rid of that super.onAttach()


On Jun 22, 1:03 am, kuku <kukuda...@googlemail.com> wrote:
> Heres my example (a custom widget):
>
> public class TestWidget extends Composite {
>
>         private static TestWidgetUiBinder uiBinder = GWT
>                         .create(TestWidgetUiBinder.class);
>
>         interface TestWidgetUiBinder extends UiBinder<Widget, TestWidget> {
>         }
>
>         @UiField
>         Button button;
>
>         public TestWidget(String firstName) {
>                 initWidget(uiBinder.createAndBindUi(this));
>                 button.setText(firstName);
>                 super.onAttach();
>         }
>
>         @UiHandler("button")
>         void onClick(ClickEvent e) {
>                 Window.alert("Hello!");
>         }
>
> }
>
> And here the code to add the Widget to a Panel:
>
> TestWidget testWidget = new TestWidget("myTestWidget");
> RootPanel.getBodyElement().appendChild(testWidget.getElement());
> mainPanel.getElement().appendChild(testWidget.getElement());
>
> I know that it is possible to add an Widget directly to an RootPanel
> like this:
> RootPanel.get("myId").add(testWidget );
>
> However lets say i want to add widgets directly to the DOM. I don't
> really understand why i have to call super.onAattch(); to get my
> ClickEvent to work. If i don't execute that line it won't fire. I'm
> not sure if this is the right way maybe somebody can give me more
> information.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment