Label.wrap doesn't work in development mode

168 views
Skip to first unread message

Evgeny V Mustafin

unread,
Feb 18, 2012, 9:18:57 AM2/18/12
to google-we...@googlegroups.com
public class NewRegAuthAlert extends FlowPanel {
   public NewRegAuthAlert() {
       String html = "For addmin comments please <div id=\"login2\">sign in</div> or register";
       add(new HTML(html));
   }

   protected void onLoad() {
       Label.wrap(DOM.getElementById("login2")).addClickHandler(new ClickHandler() {
           @Override
           public void onClick(ClickEvent clickEvent) {
                Window.alert("alert message");
           }
      });
   }
}

In dev mode this doesn't work. 
java.lang.AssertionError: A widget that has an existing parent widget may not be added to the detach list
at com.google.gwt.user.client.ui.RootPanel.detachOnWindowClose(RootPanel.java:136)
at com.google.gwt.user.client.ui.Label.wrap(Label.java:130)
But when compiled works fine. Please help.

Thomas Broyer

unread,
Feb 18, 2012, 10:40:05 AM2/18/12
to google-we...@googlegroups.com
This is the expected behavior (assertions are disabled by default in prod mode); you're not supposed to use .wrap() that way.

In your case, you can either:
  • use an HTMLPanel: grab the text inside the element to construct the Label, then use addAndReplace to replace the <div> with the newly constructed label
  • add the ClickHandler on the HTML and check the actual target of the ClickEvent to be the <div>
  • much simpler and easier to maintain (so, much better for the long term): refactor your code to use UiBinder and put a <g:HTMLPanel>For admin comments please <g:Label ui:field="login2">sign in</g:Label> or register</g:HTMLPanel> with a @UiHandler("login2") to listen to the clicks on the label.
Reply all
Reply to author
Forward
0 new messages