Issue with reusing widget in Tab Panel

17 views
Skip to first unread message

zalym

unread,
Feb 21, 2007, 3:51:32 AM2/21/07
to Google Web Toolkit
I have created a search widget which has a text box and a list box. I
want to search for 3 type of entities. So instead of creating 3
search widgets, I thought of reusing a single widget.

When I add it to a tab panel, one the third attempt i get the
following error. 2 are fine. but anything more than 2 gives an
error.

Works fine:
TabPanel tp = new TabPanel();
tp.add(customerSrchWidget, "A");
tp.add(customerSrchWidget, "B");

Gives error ==>
TabPanel tp = new TabPanel();
tp.add(customerSrchWidget, "A");
tp.add(customerSrchWidget, "B");
tp.add(customerSrchWidget, "C");
tp.add(customerSrchWidget, "D");

[ERROR] Unable to load module entry point class
com.zafinbank.rm.client.search.SearchModule
java.lang.IndexOutOfBoundsException: null
at com.google.gwt.user.client.ui.DeckPanel.insert(DeckPanel.java:77)
at com.google.gwt.user.client.ui.TabPanel.insert(TabPanel.java:154)
at com.google.gwt.user.client.ui.TabPanel.insert(TabPanel.java:165)
at com.google.gwt.user.client.ui.TabPanel.add(TabPanel.java:85)
at
com.zafinbank.rm.client.search.SearchModule.onModuleLoad(SearchModule.java:
30)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:138)
at
com.google.gwt.dev.shell.BrowserWidget.attachModuleSpace(BrowserWidget.java:
313)

Attached is the SearchWidget class
public class SearchWidget extends Composite {
private TextBox searchBox;
private ListBox searchOptions;
private boolean hasOptions=true;
public SearchWidget(Map options) {
hasOptions = options != null && options.size()!=0;
searchBox = new TextBox();
searchBox.setTitle("Enter your search criteria here");

searchOptions = new ListBox();
if(hasOptions){
Set keys = options.keySet();
for(Iterator iterator = keys.iterator();iterator.hasNext();){
String key = (String) iterator.next();
searchOptions.addItem(key, (String) options.get(key));
}
}
FlowPanel fPanel = new FlowPanel();
fPanel.add(searchBox);
if(hasOptions) fPanel.add(searchOptions);

initWidget(fPanel);
}

public String getSearchText(){
return searchBox.getText();
}

public String getSearchType(){
String type=null;
if(hasOptions)
type=searchOptions.getValue(searchOptions.getSelectedIndex());
return type;
}
}

Luciano Broussal

unread,
Feb 21, 2007, 4:03:52 AM2/21/07
to Google Web Toolkit
Hi,

You can not add the same widget to time as a Tab, you have to create
new instances of this widget before adding it.

Regards.
---
http://www.gwtwindowmanager.org

zalym

unread,
Feb 21, 2007, 4:19:47 AM2/21/07
to Google Web Toolkit
Thanks Luciano. Any idea why that would be? And why did it work for
two additions and not more?

I see that the markup generated is all table based, which is not the
preferred way to do layouts. Thats the reason for the resuse.

On Feb 21, 1:03 pm, "Luciano Broussal" <luciano.brous...@gmail.com>
wrote:


> Hi,
>
> You can not add the same widget to time as a Tab, you have to create
> new instances of this widget before adding it.
>
> Regards.

> ---http://www.gwtwindowmanager.org

Luciano Broussal

unread,
Feb 21, 2007, 6:58:28 AM2/21/07
to Google Web Toolkit
Welcome,

but i'm also surprise it works with 2 !

Luciano

zalym

unread,
Feb 21, 2007, 8:26:24 AM2/21/07
to Google Web Toolkit
Why does this work this way? Where is the issue? I think this will
help me understand the arch better.

On Feb 21, 3:58 pm, "Luciano Broussal" <luciano.brous...@gmail.com>

Ian Bambury

unread,
Feb 21, 2007, 9:01:39 AM2/21/07
to Google-We...@googlegroups.com
Hi,
You are hitting two problems here.
First:
You are creating a single object and then trying to add it to a number
of different places in the DOM tree. In the real world, if I have only
one lantern and hang it on a tree branch, then if you then tell me to
hang it on another branch, I have to take it off the first branch in
order to put it on another. That is what you are doing with just one
customerSrchWidget, you are jst moving it about. If you create a
number of customerSrchWidgets, then you can have them in different
places, of course.

Second (don't worry if you don't follow this, just create a new widget
every time and all will be OK)

The tab panel has a kind of a bug, but this bug only appears when you
are doing something wrong, so it doesn't really matter. It works like
this:

When blank has two 'invisible' tabs (for padding the left and right
edges of the line of tabs) [-L-][-R-]

It also has a WidgetCollection - a list of widgets in the tab.

First add, it gets the number of widgets in the WidgetCollection (0)
adds one to it (1) and adds it before tab 1 - you get [-L-][-W-][-R-]

Second add, it gets the number of widgets in the WidgetCollection (1
adds one to it (2) and adds it before tab 2 - you get [-L-][-W-][-R-]
again since it adds it before [-R-], but the first tab [-W-]
disappears because you are effectively moving your widget around

However, the WidgetCollection now has two references to the SAME widget

Third add: it gets the number of widgets (2) adds one to it (3) and
tries to add it before tab 3 but there are only three tabs (0, 1, and
2) so you get a reference to an index which doesn't exist.

Ideally, the tab panel (and anything else) would check if you are
adding something twice and not let you do it. More ideally, every
widget which adds another widget would check to see if the added
widget was attached to another widget first, and cleanly remove it if
so. For one reason or another, this doesn't happen, so there is no
safety net and you have to do it yourself. All that really happens is
that you get an error message which is more cryptic than it could be.

HTH,

Ian

zalym

unread,
Feb 21, 2007, 9:32:49 AM2/21/07
to Google Web Toolkit
wow.. thanks a bunch Ian. you taught me how to fish...:)

Get it loud and clear...

Ian Bambury

unread,
Feb 21, 2007, 10:35:17 AM2/21/07
to Google-We...@googlegroups.com
"Give a man a fire, and he will be warm for a day. Set a man on fire,
and he will be warm for the rest of his life." :-)

Ian

On 21/02/07, zalym <mohamme...@gmail.com> wrote:
>

Reply all
Reply to author
Forward
0 new messages