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?
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();
}
}
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.
精彩评论