Stefan
Stefan

Reputation: 14893

GWT: Delete Content of RootPanel(id)

I'm trying to delete the whole content of an RootPanel element based on an ID. The RootPanel returns correct and I can see it's content in the debugger. The problem is, that I delete it I tried the following things:

                RootPanel rp = RootPanel.get("LayoutID2");
            if (rp != null) {
                for (Widget widget : rp) {
                    rp.remove(widget);
                }
            } 

Any idea what I'm missing, or is there another function?

best regards, Stefan

Upvotes: 3

Views: 2774

Answers (3)

sage88
sage88

Reputation: 4574

The accepted answer is correct in that all of the content in the RootPanel may not be widgets. It is also correct that rp.clear() will remove all of the widgets. However, if you want to do what the second answer suggests and completely clear the panel, there is a very easy approach that is not given here. Use:

rp.clear(true);

The optional boolean parameter will clear the DOM elements in the RootPanel as well as the Widgets. This is much simpler than the second answer.

Upvotes: 1

Byorn
Byorn

Reputation: 721

The above answer is incorrect :D !

RootPanel.clear() removes widgets from the panel. I think you want to clear out existing HTML elements. RootPanel.get() automatically did this in 1.0.21, but this functionality was removed in 1.1. I use the following utility function to accomplish this:

public void clear(Element parent)
{
        Element firstChild;
        while((firstChild = DOM.getFirstChild(parent)) != null)
        {
                DOM.removeChild(parent, firstChild);
        }
}

Upvotes: 3

Tahir Akhtar
Tahir Akhtar

Reputation: 11645

All of the contents of the RootPanel might not be widgets. For example if you placed following html in your host page:

<div id="LayoutID2">
   Here goes the dynamic content
</div>

The text "Here goes the dynamic content" will not appear as a widget.

By the way, removal of all the widgets can be achieved by calling rp.clear().

Upvotes: 5

Related Questions