View Full Version : [CLOSED] Unable to add more than one widget to a ContentPanel?

14 Nov 2011, 7:09 AM
Feature or bug?

Here's a very simple example code:

ContentPanel view = new ContentPanel();

TextButton btn1 = new TextButton("1");
TextButton btn2 = new TextButton("2");


System.out.println("view.getWidgetCount() = " + view.getWidgetCount()); // = 1

The problem is, only the last widget (btn2) is really rendered. getWidgetCount() returns 1, as if btn1 was never added.
If it's a feature (like "you have to add another panel wrapping all buttons") - what would be getWidgetCount() for?

14 Nov 2011, 7:28 AM
ContentPanel is a SimpleContainer and so only contains one child at a time. You need to wrap them in a VerticalLayoutContainer for example

15 Nov 2011, 4:15 AM
Thank you, I didn't notice the new SimpleContainer concept. I guess, it should be fixed/improved though, since the SimpleContainer.add() method says:

The expectation is that this method would be overridden by a child object.
And that's what ContentPanel (and other SimpleContainer subclasses) don't do. And therefore it's not very intuitive, especially if you want to migrate your 2.x application, where you could use ContentPanel.add() to add more than one widget.

Also, as mentioned, inherited methods like "getWidgetCount()" or "getWidget(int)" lead to even more confusion. Seeing this method on a SimpleContainer make a developer assume, it could actually contain more than one widget. Making those methods private, for example, would prevent such assumptions.

15 Nov 2011, 4:29 AM
Theses methods are comming from a GWT interface.

In GXT 3 the layouts are bound into the containers. We are still working on adding more javadocs, however there are currently no plans to change the design. Contentpanel does not have any insert method. SimplePanel from GWT behaves also exactly the same way, only that it will throw an exception.

If you take a look at Widget.removeFromParent you will see that the HasWidget interface (that has the add method) is required).

If you require to handle more than one widget, you need to add another container into the ContentPanel, like a FlowLayoutContainer.

In GXT 2 the add method maybe always works. However when using a FitLayout only the first child would have been used, same in a CenterLayout. In GXT 3, the container takes care of all this.