PDA

View Full Version : Inserting a component into an Div



kaushikGXT
14 Dec 2011, 7:04 AM
I am trying to create a div and insert a component into the div by as follows and have not been successful. What is it that I am doing incorrectly?

HorizontalPanel hp = new HorizontalPanel();
Html myHtml = new Html("<div id=\"htmlDiv\""></div>");
Label myLabel = new Label("Name");

hp.add(myHtml);
RootPanel.get("htmlDiv").add(myLabel);

sven
14 Dec 2011, 7:12 AM
RootPanel.get("htmlDiv").add(myLabel);

That will only work, when the element with the id "htmlDiv" is already in the page.

kaushikGXT
14 Dec 2011, 7:16 AM
Is there any other way to add the new Html element and to induce a component into it? Would forcing the hp.layout() help?


HorizontalPanel hp = new HorizontalPanel();
Html myHtml = new Html("<div id=\"htmlDiv\""></div>");
Label myLabel = new Label("Name");

hp.add(myHtml);
hp.layout();

RootPanel.get("htmlDiv").add(myLabel);

Thanks,
Kaushik

sven
14 Dec 2011, 7:29 AM
Another solution would be to use HtmlContainer. However this depends on your exact requirements.

kaushikGXT
14 Dec 2011, 8:10 AM
I tried to add the label to the div, after the page was rendered and still could not find the div on the page.

private HorizontalPanel getHP(){


HorizontalPanel hp = new HorizontalPanel();

Html myHtml = new Html("<div id=\"htmlDiv\""></div>");

hp.add(myHtml);
return hp;
}

@Override
protected void afterRender() {
// TODO Auto-generated method stub
Label listLabel = new Label("Sort By:");
listLabel.setId("listLabelId");
if (RootPanel.get("leftDiv") != null){
RootPanel.get("leftDiv").add(listLabel);
} else {
Window.alert("Cannot find the div");
}
}


Is there anything else that I need to be looking into.

sven
14 Dec 2011, 8:14 AM
Can you please provide a fully working testcase implementing EntryPOint? From what are you overrding afterRender?