cometta
cometta

Reputation: 35679

gwt dynamic adding textbox

 @UiHandler("addDynamicTextboxbutton")
  void addMoreTextbox(ClickEvent event) {


     textboxplaceholder.add(new TextBox(),"textboxplace");

 }

when addDynamicTextboxbutton button is clicked, this method is executed and new textbox is created. how to have another button, so that when click, will get all the values from each of the "textbox()" ? or is it necessary to put name "new textbox("name")" so that i can get all their values? what is the best practice way to do this?

Upvotes: 1

Views: 1716

Answers (2)

Hilbrand Bouwkamp
Hilbrand Bouwkamp

Reputation: 13519

If textboxplaceholder is and extend of a ComplexPanel, for example FlowPanel you could simply iterate over the children of the FlowPanel:

for (Widget w: textboxplaceholder) {
   if (w instanceof TextBox) {
     // do something value: ((TextBox)w).getValue();
   }
}

Upvotes: 2

z00bs
z00bs

Reputation: 7498

You could use a collection to store the newly added TextBox from which you can later get all the values. Something like this:

public class Test extends Composite {

    private static TestUiBinder uiBinder = GWT.create(TestUiBinder.class);

    interface TestUiBinder extends UiBinder<Widget, Test> {}

    @UiField
    FlowPanel textboxplaceholder;
    @UiField
    Button addDynamicTextboxbutton;
    private LinkedList<TextBox> boxes;

    public Test() {
        initWidget(uiBinder.createAndBindUi(this));
        boxes = new LinkedList<TextBox>();
    }

    @UiHandler("addDynamicTextboxbutton")
    void addMoreTextbox(ClickEvent event) {
        TextBox box = new TextBox();
        textboxplaceholder.add(box);
        boxes.add(box);
    }
}

Iterate over the boxes stored in boxes to get all the values.

Upvotes: 1

Related Questions