tags:

views:

518

answers:

3

This may have been asked before but I have no idea how to word it to search for it.

I have a composite widget that has methods to update some of the widgets that make up the composite widget. When I add this composite widget to my panel I use a do while loop to pull data from an XML file and populate the composite data. When I instantiate the object each time to add the data it has a scope local to the do-while loop and I cannot call methods to update the data in the composite widget later on. Is there maybe a way to make an array of these composite widgets or another solution to be able to access the Widget?

Eric

+3  A: 

Sure... use

List<Composite> widgetList = new ArrayList<Composite>();
// loop
widgetList.add(widget);
// end loop
widgetList.get(3).toString();

You'll want to use your custom class instead of Composite in the list generic... there's nothing stopping you from making data structures using widgets, just like you would with any other Java class.

Sudhir Jonathan
thanks for the help! worked great.
Eric Dorsey
Then it would help if you marked the answer as correct and closed the question :D
Sudhir Jonathan
+1  A: 

I definately recommend that you watch "Best Practices for Architecting GWT App" (from Google I/O 2009):

http://www.youtube.com/watch?v=PDuhR18-EdM

At about 24 minutes through it talks about how to write composite widgets using the MVP design pattern - although you should watch it all. Unfortunately it does not provide ready to use code snipets, but it does show you how to construct a framework to decouple your XML and UI objects nicely.

Thanks for the information, I will take a look.
Eric Dorsey
A: 

If you are putting all your Widgets in that loop into one panel (presumably on of the subclasses of ComplexPanel, since you are adding many Widgets to one panel), then you could use one of the methods to access Widgets contained within a panel (assuming you add only those XML generated Widgets to the panel and nothing more):

  • com.google.gwt.user.client.ui.ComplexPanel.iterator() - returns an java.util.Iterator<Widget> you can use to traverse the list of Widgets within that Panel
  • com.google.gwt.user.client.ui.ComplexPanel.getWidgetCount() and getWidget(int index) can be used in a for loop to go through all the Widgets within a panel

So, let's look at an example:

VerticalPanel vPanel = new VerticalPanel();

// Fill up the vPanel using XML

Iterator<Widget> iterator = vPanel.iterator();

while(iterator.hasNext()) {
    Widget w = iterator.next();
    // Do something with w
}

// Or...
for (int i = 0; i < vPanel.getWidgetCount(); i++) {
    Widget w = vPanel.getWidget(i);
    // Do something with w
}

Of course, substitute VerticalPanel with the one you are you using :)

Igor Klimer