tags:

views:

234

answers:

2

HTMLPanel inherits the add(Widget) method from com.google.gwt.user.client.ui.Panel, but doesn't override it. This leads to an UnsupportedOperationException when called.

Would there be any problems if it was overriding it like FlowPanel does?

@Override
public void add(Widget w) {
  add(w, getElement());
}

Background: HTMLPanel can be constructed faster than FlowPanel (innerHTML vs DOM manipulation). But after construction, it doesn't currently allow to add elements dynamically*.

* except for the not so useful method add(Widget widget, String id) , which makes it impossible to use the HTMLPanel twice in a document.

+1  A: 

I'm not sure rationale behind not including the trivial override, but you can call add(Widget, Element) yourself if you wish.

I think one of the ideas with HTMLPanel is that you could add a widget anywhere in the DOM of the panel, so it needs add() methods that force you to specify where exactly to add the widget. This way you can construct the HTMLPanel with some complex html like: "<div>...<div id="container"/>...<div>", then call add(myWidget, "container").

btw: add(Widget w, String id) is just a convience method. It doesn't create a new element with an id, but looks up the element with the id and calls add(Widget, Element).

spankalee
+1 "one of the ideas with HTMLPanel is that you could add a widget anywhere in the DOM of the panel" Good explanation, makes sense. I wonder, if I must be careful when calling the protected method add(Widget, Element). I certainly don't want to mess up GWT's event handling and auto cleanup (http://code.google.com/p/google-web-toolkit/wiki/DomEventsAndMemoryLeaks)
Chris Lercher
add(Widget, Element) will take care of the bookkeeping needed for the cleanup of event handlers and such.You might be interested in UIBinder. When you write an HTMLPanel in UIBinder you can just place widgets inline without any hassle.
spankalee
A: 

Regarding the add(Widget widget, String id) - this method works only for elements defined within the HTMLPanel - as stated in javadoc (and I have also verified that on my machine).

How I understand it from the javadoc addAndReplaceElement(Widget widget, java.lang.String id) will work on any element but still will attach the new widget to HTMLPanel (basically HTMLPanel would handle events while the widgets physical location would be determined by the location of the removed element).

markovuksanovic
The thing is: I want to add elements (widgets) as *direct* DOM children to the HTMLPanel's div. Also, I can't use IDs, because I reuse the same HTMLPanel (built by UiBinder) multiple times in the document...
Chris Lercher