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);
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
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.
Can you please provide a fully working testcase implementing EntryPOint? From what are you overrding afterRender?
Powered by vBulletin® Version 4.1.5 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.