Jorge Lazo
Jorge Lazo

Reputation: 388

Adding content to a div in HTML using GWT

I am pretty new to GWT, and I have these dynamic divs setup on my starting GWT html page:

<div id="nav">
Navigation goes here...
</div>

<div id="core">
</div>

<div id="footer">
footer info goes here
</div>

I have my main class that implements EntryPoint and ValueChangeHandler to dynamically change the "core" div so it renders different information based on what the user clicks on the main page (so it mimicks using different pages, but only using one through History and Hyperlink classes Like this). I tried doing this with something like:

        VerticalPanel panel = new VerticalPanel();
        HTML mapHtmlDiv = new HTML("<div id=\"maps\"></div>");
        panel.add(mapHtmlDiv);
        RootPanel.get("core").add(panel);
        // add a widget to the maps div downstream

Once the condition is met. However this is not allowed on GWT, as it throws an error along the lines of "A widget that has an existing parent widget may not be added to the detach list", what is the correct way of adding content and divs to HTML?

Upvotes: 0

Views: 3229

Answers (2)

Tobika
Tobika

Reputation: 1062

VerticalPanel panel = new VerticalPanel();
HTML mapHtmlDiv = new HTML("<div id=\"maps\"></div>");
panel.add(mapHtmlDiv);

Element coreDiv = DOM.getElementById("core");
coreDiv.appendChild(panel.getElement());

Upvotes: -1

Mohit
Mohit

Reputation: 1755

HTML widget in GWT is not a container. This means you can't add widgets into it. Use HTMLPanel instead.

VerticalPanel panel = new VerticalPanel();
HTMLPanel mapPanel = new HTMLPanel(""); //automatically creates a div.
panel.add(mapPanel);
RootPanel.get("core").add(panel);
// add a widget to the maps div downstream
mapPanel.add(new Label("Sample label"));

http://www.gwtproject.org/javadoc/latest/com/google/gwt/user/client/ui/HTMLPanel.html

Upvotes: 2

Related Questions