package miw2;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
 
/**
 * Class contains list of RadioButtons and OK button.
 * @author  Filip
 */
public class MyRBForm extends javax.swing.JFrame {
 
    /**
     * List of selected values, always one can be selected at once.
     */
    public static List<String> checked;
    /**
     * List of RadioButtons.
     */
    public static List<JRadioButton> cBox;
    /**
     * Used to avoid selecting more than one RadioButton at once.
     */
    ButtonGroup group;
    /**
     * Returned value,can be from 1 to number of RadioButtons.
     */
    public static int retVal;
 
    /**
     * Create RadioButton Form. By default first RadioButton is selected.
     * @param list list of values to choose.
     * @param title question.
     */
    public MyRBForm(String[] list, String[] title) {
        retVal = 0;
        group = new ButtonGroup();
        if (cBox == null) {
            cBox = new ArrayList<JRadioButton>();
        } else {
            cBox.clear();
        }
        Box bx = Box.createVerticalBox();
        for (int i = 0; i < list.length; i++) {
            cBox.add(new JRadioButton(list[i]));
            group.add(cBox.get(i));
            bx.add(cBox.get(i));
            if(i == 0) cBox.get(i).setSelected(true);
        }
        this.add(bx);
        this.setTitle(title[0]);
        this.pack();
        this.setDefaultCloseOperation(MyRBForm.DO_NOTHING_ON_CLOSE);
        this.setResizable(false);
        initComponents();
        this.setLocationRelativeTo(null);
    }
 
    /**
     * Do loop until button OK is pressed.
     * @return number of selected RadionButton.
     */
    public static int getValue() {
        while(retVal == 0){
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                Logger.getLogger(MyRBForm.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return retVal;
    }
 
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
 
        jButton1 = new javax.swing.JButton();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 
        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
 
        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(247, Short.MAX_VALUE)
                .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 96, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(285, Short.MAX_VALUE)
                .add(jButton1)
                .addContainerGap())
        );
 
        pack();
    }// </editor-fold>                        
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        checked = new ArrayList<String>();
        Iterator it = cBox.iterator();
        int i=1;
        while (it.hasNext()) {
            JRadioButton box = (JRadioButton) it.next();
            if (box.isSelected()) {
                checked.add(box.getText());
                retVal =i;
            }
            i++;
        }
        this.dispose();
    }                                        
 
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
 
            public void run() {
                new MyRBForm(new String[]{"jeden", "dwa","trzy"}, new String[]{"Title"}).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    // End of variables declaration                   
}