title: How to create dynamic radiobuttons in CodenameOne
body: <p>Using CodenameOne, I created an array of RadioButtons based on the count of records on a database table. </p>
  
  <pre><code>Container searchDisplayContainer = findMyContainer(f);
  
  for(int i=0; i &lt; records.length; i++) {
      RadioButton rdb = new RadioButton();
  
      rdb.setName("rdb"+i);
  
      rdb.setText("rdb"+i);
  
      rdb.setGroup("locations"); 
  
      searchDisplayContainer.addComponent(rdb);
  }
  </code></pre>
  
  <p>With this code, a number of RadioButtons are displayed on the simulator screen.</p>
  
  <p>My problem is that after displaying the radiobuttons, I cannot check which one is selected.</p>
  
  <p>Usually I can create the radiobutton in the theme.res and call it in the code with:</p>
  
  <pre><code>RadioButton rdb1 = findRadioButton(f);  
  </code></pre>
  
  <p>and check if the button is selected with if(rdb1.isSelected)</p>
  
  <p>However since I am not creating my radio buttons initially in the theme.res, I cannot use the <code>findRadioButton(f)</code> method.</p>
  
  <p>My question is how do I create a number of RadioButtons within the code and then be able to check if they are selected after the submit button is pressed?</p>
  
  <hr>
  
  <h2>Question Modification</h2>
  
  <pre><code>package userclasses;
  
  import generated.StateMachineBase;
  import com.codename1.ui.*; 
  import com.codename1.ui.events.*;
  import com.codename1.ui.util.Resources;
  
  /**
  *
  * @author Your name here
  */
  public class StateMachine extends StateMachineBase {
  
      private Container searchDisplayContainer;
      private final int recordLength = 5;
      private ButtonGroup bg = new ButtonGroup();
      private RadioButton[] rdbs = new RadioButton[recordLength];
  
      public StateMachine(String resFile) {
          super(resFile);
      }
  
      /**
       * this method should be used to initialize variables instead of
       * the constructor/class scope to avoid race conditions
       */
      protected void initVars(Resources res) {
      }
  
      @Override
      protected void beforeMain(Form f) {
          searchDisplayContainer = findContainer1(f);
  
          for(int i=0;i&lt;recordLength;i++){
              rdbs[i].setName("rdb"+i);
              rdbs[i].setText("rdb"+i);
              //add to button group
               bg.add(rdbs[i]);    
               //add to container
              searchDisplayContainer.addComponent(rdbs[i]);
  
  
          }
      }
      @Override
      protected void onMain_ButtonAction(Component c, ActionEvent event) {
          System.out.println(bg.getSelectedIndex());
      }
     }
  </code></pre>