GWT: how to get the selected switch value

I create a dynamic number of radio buttons in my GWT

      public void createTestList(ArrayList<Test> result){
    for(int i =0 ; i<result.size();i++){
                    int id = result.get(i).getTestId();
        RadioButton rd = new RadioButton("group", result.get(i).getTestType());
        verticalPanel.add(rd);
    }

where Test is my Entity class.

I get 4 different types of radio buttons in my view. Now, if I select any of the switches, first I need to get the identifier of the selected radio button, how is this possible?

secondly How can I check which of several radio buttons is selected?

thank

+3
source share
2 answers

You need to check public java.lang.Boolean getValue () on each switch, whether it is checked or not.

+2
source

you can add a click handler and update the selected switch variable:

choiceItemKind = new VerticalPanel();
ArrayList<String> kinds = new ArrayList<String>();
kinds.add(...);
kinds.add(...);

choiceItemKind.clear();

ClickHandler choiceClickHandler = new ClickHandler()
{
    @Override
    public void onClick(ClickEvent event)
    {
        addItemKindSelectedLabel = ((RadioButton) event.getSource()).getText();
    }
};

for (String label : kinds)
{
    RadioButton radioButton = new RadioButton("kind", label);
    //radioButton.setTitle("Tooltyp");
    if (label.equals(addItemKindSelectedLabel))
        radioButton.setValue(true);
    radioButton.addClickHandler(choiceClickHandler);
    choiceItemKind.add(radioButton);
}
...
addItemKindSelectedLabel = "";
...
if (!addItemKindSelectedLabel.isEmpty())
    ...;

upd: set the selected radio button without rebuilding:

for (int i = 0; i < choiceItemKind.getWidgetCount(); i++)
{
    RadioButton radioButton = (RadioButton) choiceItemKind.getWidget(i);
    radioButton.setValue(radioButton.getText().equals(addItemKindSelectedLabel));
}
0
source

All Articles