I add radio buttons to the form in the following way:

for (int i=1;i<vote.size();i++){ javax.swing.JRadioButton radio = new javax.swing.JRadioButton(vote.get(i).toString()); jPanel4.add(radio, i-1); buttonGroup1.add(radio); jPanel4.validate(); } 

How after that to learn the selected index?

vote - a list of signatures to the buttons. And the number of signatures is the number of buttons.

    2 answers 2

    declare i final, and in the listener use a modification of your example:

     for (int i=1;i<vote.size();i++){ final fI = i; javax.swing.JRadioButton radio = new javax.swing.JRadioButton(vote.get(i).toString()); radio.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //fI - доступна для использования } }); jPanel4.add(radio, i-1); buttonGroup1.add(radio); jPanel4.validate(); } 
    • Swears by i ++; - Ilmirus
    • Yes exactly. corrected - Vitaliy

    Use listeners, for example, as shown .