PDA

View Full Version : Add Dynamically Component to a Container



chaby
28 Aug 2009, 1:23 AM
I try to add dynamically Component to a Container.
I want to display a Component to a LayoutContainer when i click to a Button for example, see my code bellow:


package somePackage;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.RootPanel;
import com.extjs.gxt.ui.client.widget.button.Button;

/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class TestGWT implements EntryPoint
{
/**
* This is the entry point method.
*/
public void onModuleLoad()
{
Viewport viewport = new Viewport();
viewport.setLayout(new BorderLayout());

LayoutContainer layoutContainer1 = new LayoutContainer();
layoutContainer1.setLayout(new FlowLayout());

Button button = new Button("Test");
layoutContainer1.add(button);

final LayoutContainer layoutContainer2 = new LayoutContainer();
layoutContainer2.setLayout(new FlowLayout());


viewport.add(layoutContainer1, new BorderLayoutData(LayoutRegion.NORTH));
viewport.add(layoutContainer2, new BorderLayoutData(LayoutRegion.CENTER));

button.addSelectionListener(new SelectionListener<ButtonEvent>()
{

@Override
public void componentSelected(ButtonEvent ce)
{
Info.display("Click", ce.getButton().getItemId() + " clicked");
// layoutContainer2.fireEvent(Events.BeforeAdd);
Button hc = new Button("Hello");
layoutContainer2.add(hc);
// layoutContainer2.fireEvent(Events.Add);
}

});

RootPanel.get().add(viewport);
}
}

Any ideas?
Thanks in advance

chaby
28 Aug 2009, 1:52 AM
Ok i just solve it, call layout() method of the container after the add:


Info.display("Click", ce.getButton().getItemId() + " clicked");
Button hc = new Button("Hello");
layoutContainer2.add(hc);
layoutContainer2.layout();