Re: Destroy a widget

580 views
Skip to first unread message

Benjamin Possolo

unread,
Oct 29, 2012, 4:43:41 PM10/29/12
to google-we...@googlegroups.com
I presume by uibinder owner you mean your HelloWorld class overrides the onEvent() method, is that correct?

I also presume that you have some other component that is using the HelloWorld objects and attaching them to the view.
For example, in your EntryPoint:

HelloWorld h1 = new HelloWorld();
HelloWorld h2 = new HelloWorld();

FlowPanel panel = new FlowPanel();
panel.add(h1);
panel.add(h2);

GWT.getRoot().add(panel);

If you want to "destroy" h1, and have it stop reacting to events, simply remove it from the dom and ensure it is garbage collected (ie. set your reference to null):

panel.remove(h1);
h1 = null;

Benjamin Possolo

unread,
Oct 29, 2012, 4:46:02 PM10/29/12
to google-we...@googlegroups.com
Sorry there is a typo there.

GWT.getRoot().add(panel);
should be
RootPanel.get().add(panel);

Jens

unread,
Oct 29, 2012, 4:46:34 PM10/29/12
to google-we...@googlegroups.com
When you create two HelloWorld widgets each of them receives its own events. If you use @UiHandler in your UiBinder widget and you want to stop receive events for a widget without removing the widget itself from the parent (for whatever reason) you have to "disable" your @UiHandler implementation.

So you would end up having

@UiHandler(...)
void onEvent(...) {
  if(handleEvents) {
     //do your event logic
  }
}

or you use a Delegate interface that a class can implement and that contains your event logic implementation:

@UiHandler(...)
void onEvent(...) {
  if(delegate != null) {
     delegate.onEvent();
  }
}

In that case you would disable your events by nulling your delegate: widget.setDelegate(null)


Otherwise you have to remove the widget from the parent.


-- J.

Jose María Zaragoza

unread,
Oct 30, 2012, 7:58:34 AM10/30/12
to google-we...@googlegroups.com


If you want to "destroy" h1, and have it stop reacting to events, simply remove it from the dom and ensure it is garbage collected (ie. set your reference to null):

panel.remove(h1);
h1 = null;

Thanks

Garbage collected? I thought that we were in a Javascript world
Setting to null doesn't work like I want because it's Javascript code.

And I don't want to remove it from a panel , but "destroy it" as entity. Maybe that doesn't have any sense, because I don't know how is implemented by GWT compiler into Javascript code, but  h1=null doesn`t work for me



Jose María Zaragoza

unread,
Oct 30, 2012, 8:00:48 AM10/30/12
to google-we...@googlegroups.com
Thanks for you answer. 
But  "to remove the widget from the parent." doesn`t avoid to receive events ( or my tests were wrong )

Benjamin Possolo

unread,
Oct 31, 2012, 5:15:30 PM10/31/12
to google-we...@googlegroups.com
I can't really understand what you are asking or saying.

If you want the widget to remain visible but to stop receiving events, do what Jens said: suppress the event handling logic in the event handler.
If you want the widget to stop being visible and to stop receiving events, just do what I said: remove the widget from it's container.

and yes, javascript also has garbage collection otherwise every web application would be one giant memory leak
Reply all
Reply to author
Forward
0 new messages