tags:

views:

532

answers:

1

I have a Composite whose main widget has two children, thus:

public MyComposite() {
 child1 = new FlowPanel();
 child1.getElement().setId("child1");

 child2 = new FlowPanel();
 child2.getElement().setId("child2");

 panel = new FlowPanel();
 panel.add(child1);
 panel.add(child2);
 initWidget(panel);
}

Some time after construction of MyComposite, I want to swap child1 out, replacing it with another widget, new-child1.

I could perhaps remove child1 by calling panel.remove(child1), and then add my new widget by calling panel.add(new-child1); but this would cause child2 to be rendered first, wouldn't it?

So, how can I replace child1 with new-child1 without changing the order of panel's children?

+4  A: 

Try insert() instead of add().

Unfortunately, you can't call insert() since it's protected, so you need to extend FlowPanel:

public class UsefulFlowPanel extends FlowPanel {
    public void add (int index, Widget child) {
        insert (child, getElement(), index, true);
    }
}

should work.

Aaron Digulla
Last time I looked insert was a public method: http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/gwt/user/client/ui/FlowPanel.html#insert%28com.google.gwt.user.client.ui.Widget,%20int%29
grigory
Hmm... Google must have returned an old version of the docs for me, then.
Aaron Digulla
Wasn't public in 1.5: http://google-web-toolkit.googlecode.com/svn/javadoc/1.5/com/google/gwt/user/client/ui/FlowPanel.html
Aaron Digulla
nice call for Google then...
grigory