On 22 juin, 00:03, kuku <[email protected]> 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();
>         }

It means that:
 1. TestWidget can only be added to the page by adding its element to
another element, i.e. not as a child of another widget (this would
cause onAttach to be called, which will fail with an
IllegalStateException because it's already attached by this
super.onAttach call in the constructor)
 2. if you happen to add a widget as a child of TestWidget afterwards
(i.e. TestWidget is a container), the child widget will fail to
receive events (because its onAttach won't be called)
 3. you can't extend TestWidget and override onAttach, as you're
calling super.onAttach here which bypasses virtual dispatch.

>         @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());

The onAttach should be called from here (but it is protected, so you
can't, unless you override it to widen its visibility).

> 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.

Actually, if you're absolutely sure you *need* such a thing (not only
"want", but "need" it), I'd make static append(Element) methods
similar to wrap(Element) that exists in some widgets already, just
that instead of using the given element as the widget's element, you
append the widget's element as a child of the given element. That'd be
much cleaner.

> I don't
> really understand why i have to call super.onAattch(); to get my
> ClickEvent to work.

Because that's the way GWT widgets work, partly to workaround memory
leaks in various browsers (er, in IE, not to name it):
http://code.google.com/p/google-web-toolkit/wiki/DomEventsAndMemoryLeaks

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

Reply via email to