propertiesPanel may contain other type of widgets. You can use
Widget widget = propertiesPanel.getWidget(i);
if (widget instanceof TextBox) {
TextBox tb = (TextBox)widget;
....
}
Jim
http://www.gwtorm.com - GWT ORM
http://code.google.com/p/dreamsource-orm/
On May 28, 7:01 am, ericv <[email protected]> wrote:
> The following code sets up a button to add text boxes to a panel
> dynamically:
>
> public void setLayout() {
> addPropertyButton = new Button("Add property");
> addPropertyButton.addClickHandler(new ClickHandler() {
> public void onClick(ClickEvent event) {
> addProperty("");
> }
> });
> }
>
> private void addProperty(String propertyName) {
> final TextBox propertyTextBox = new TextBox();
> propertyTextBox.setText(propertyName);
> propertiesPanel.add(propertyTextBox);
> // etc...
> }
>
> Let's say we wanted to retrieve the values of all text boxes in that
> container, and put them in an array. Since we didn't keep track of all
> text boxes, the obvious way to retrieve their contents would be to
> typecast the container's widgets:
>
> public ArrayList<String> getPropertiesList() {
> ArrayList<String> array = new ArrayList<String>();
> String s;
> for (int i=0; i<propertiesPanel.getWidgetCount(); i++) {
> TextBox tb = (TextBox) (propertiesPanel.getWidget(i));
> s = tb.getText();
> if (s.length()>0) {
> array.add(s);
> }
> }
> return array;
> }
>
> The above code yields a ClassCastException, which effectively makes
> the getWidget() method useless for this purpose. One workaround is to
> keep track of all the TextBox instances in a field collection, but it
> seems redundant, since we already have a container holding all the
> text box elements in it.
>
> What's the pattern for dealing with this situation? Is this a GWT bug,
> or does the javascript implementation require data holders to be
> instance variables in order to retrieve their information?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---