使用 GWT 向 HTML 中的 div 添加内容

Adding content to a div in HTML using GWT

我是 GWT 的新手,我在我的起始 GWT html 页面上有这些动态 divs 设置:

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

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

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

我的主要 class 实现了 EntryPoint 和 ValueChangeHandler 以动态更改 "core" div 因此它会根据用户在主页上单击的内容呈现不同的信息(所以它模仿使用不同的页面,但仅通过历史和超链接使用一个 classes Like this)。我试着用类似的东西来做这个:

        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

一旦满足条件。然而,这在 GWT 上是不允许的,因为它会引发 "A widget that has an existing parent widget may not be added to the detach list" 行的错误,将内容和 divs 添加到 HTML 的正确方法是什么?

HTML GWT 中的小部件不是容器。这意味着您不能向其中添加小部件。请改用 HTMLPanel。

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

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

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