Ming Leung
Ming Leung

Reputation: 385

GWT SimplePanel can only contain one child widget when working with popup

I am confused with the simple panel and its only one child widget feature.

The following will cause the exception about the one child widget. How does the popup violate the one child widget rule? If I create an inner class extends the PopupPanel, then there is no more one widget exception.

Thanks

Class DashBoardPanel extends SimplePanel{
private PopupPanel popup=new PopupPanel(true);
...
public void initDashBoardPanel(ArrayList<EventDTO> _evtAry) 
{
    Iterator<EventDTO> it=_evtAry.iterator();
    int row=0; int col=0;
    while(it.hasNext()) 
    {
        final EventDTO evt=it.next();
        //every 3 column, next row
        if(col==3)
        {
            col=0;
            row=row+1;
        }
        Image evtPic=new Image(GWT.getHostPageBaseURL()+"photoServlet?photoName="+evt.getEvtPicName());
        evtPic.setSize("200px", "200px");
        Button editBtn=new Button("Edit");
        FlexTable evtTab=new FlexTable();
        evtTab.setWidget(0, 0, evtPic);
        evtTab.setWidget(1, 0, editBtn);
        this.evtFlexTable.setWidget(row, col, evtTab);
        editBtn.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                FlexTable table =new FlexTable();
                table.setWidget(0, 0, new Label(evt.getEvtName()));
                popup.add(table);
                popup.center();
            }
        });
        col=col+1;
    }
    frame.add(new Label("DASH BOARD"));
    frame.add(this.evtFlexTable);
    this.btnPanel.setWidget(0, 0, this.btn1);
    this.btnPanel.setWidget(0, 1, this.btn2);
    frame.add(this.btnPanel);
    this.add(frame);
}

But if I create an inner class extends Popup, there is no SimplePanel only one child widget exception.

public class DashBoardPanel extends SimplePanel{
private EvtEditPopup evtEditPopup=new EvtEditPopup();
//Replaced the popup by this
public class EvtEditPopup extends PopupPanel
{
    FlexTable table =new FlexTable();
    String evtName="Event";
    //table.setWidget(0, 0, new Label(evt.getEvtName()));
    public EvtEditPopup()
    {
        super(true);
        table.setWidget(0, 0, new Label(evtName));
        this.add(table);
    }
    public void setEvtName(String _evtName)
    {
        this.evtName=_evtName;
        table.setWidget(0, 0, new Label(evtName));
    }
    public String getEvtName()
    {
        return this.evtName;
    }
}
public void initDashBoardPanel(ArrayList<EventDTO> _evtAry) 
......
editBtn.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                DashBoardPanel.this.evtEditPopup.setEvtName(evt.getEvtName());
                DashBoardPanel.this.evtEditPopup.center();
            }
        });

Upvotes: 0

Views: 308

Answers (1)

Ignacio Baca
Ignacio Baca

Reputation: 1578

There are other panels that support multiple children like FlowPanel, VerticalPanel, etc. If there are widgets like PopupPanel that only support one child, just wrap the content into one of these panels.

GWT Showcase • DialogBox, example using a VerticalPanel:

// Create a table to layout the content
VerticalPanel dialogContents = new VerticalPanel();
dialogContents.setSpacing(4);
dialogBox.setWidget(dialogContents);

Also, you should follow the composition over inheritance rule using Composite (custom widgets guide).

Upvotes: 3

Related Questions