views:

1233

answers:

3

I am attempting to create a Google Web Toolkit (GWT) application that also uses Google Gears, but every time I try to remove the panel, I get an exception and the panel stays there.

Here is an excerpt from the exception I get (I've only included the relevant bits of the call stack, the rest just descends into the included function below):

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:122)
 at com.google.gwt.user.client.ui.RootPanel.get(RootPanel.java:197)

I'm not sure what the problem is, but I really don't like leaving the button there after they approve the use of Gears.

What am I doing wrong? Or any suggestions on a different way I could do this to make it work?

if(!gearsFactory.hasPermission()) {
 HorizontalPanel rightPanel = new HorizontalPanel();
 rightPanel.getElement().setId("gearsPrompt");
 rightPanel.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
 rightPanel.setSpacing(0);
 rightPanel.setHeight("28px");

 InlineLabel enableGearsText = new InlineLabel("Enable Gears for off-line access");
 enableGearsText.getElement().setId("gearsText");
 enableGearsText.addStyleName("titleElement");
 rightPanel.add(enableGearsText);

 final Button gearsButton = new Button("Use Gears");
 gearsButton.getElement().setId("gearsButton");
 gearsButton.addStyleName("titleElement");
 gearsButton.setHeight("24px");

 gearsButton.addClickHandler( new ClickHandler() {
  public void onClick(ClickEvent event) {
   Factory gearsFactory = Factory.getInstance();
   if(gearsFactory != null) {
    if(gearsFactory.getPermission()) {
     RootPanel gearsPrompt = RootPanel.get("gearsPrompt");
     gearsPrompt.removeFromParent();
    }
   }
  }
 });

 rightPanel.add(gearsButton);

 RootPanel titleBarRight = RootPanel.get("titleBarRight");
 titleBarRight.add(rightPanel);
}
A: 

One solution I've found is to loop through all of the widgets under the "titleBarRight" panel and remove all widgets it contains:

if(gearsFactory.getPermission()) {
 RootPanel titleBarRight = RootPanel.get("titleBarRight");
 java.util.Iterator<Widget> itr = titleBarRight.iterator();
 while(itr.hasNext()) {
  itr.next();
  itr.remove();
 }
}

But somehow this still seems hacky and not quite the "right way to do it."

Miquella
A: 

Is there any reason for using RootPanel.get("gearsPrompt").removeFromParent(); instead of your own rightPanel.removeFromParent();? The reference is already there.

Robert Munteanu
Unfortunately that doesn't work.The removeFromParent() call is inside an anonymous ClickHandler object and thus is not in the same scope as rightPanel.Thanks for the idea though!
Miquella
A: 

You can do :

theParentWidget.remove(index);

and the first child corresponds to 0;

sLL4yeR