/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Capitulo_6.Dibujo3;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 *
 * @author felix
 */
public class SeleccionPanel extends JPanel implements ActionListener{
    private int seleccion;
    private String nombres[];
    
    public SeleccionPanel(String Nombre, String[] opciones, int N) {
        super(new GridLayout(N/2, 2));
        int i;
        nombres = new String[N];
        seleccion = 0;
        
        for(i=0; i<N; i++)
            nombres[i] = opciones[i];
        
        this.setBackground(Color.lightGray);
        this.setBorder(BorderFactory.createTitledBorder(Nombre));
        ButtonGroup group = new ButtonGroup();
        JRadioButton option;
        
        for(i=0; i<N; i++) {
            option = new JRadioButton(opciones[i]);
            option.addActionListener(this);
            group.add(option);
            if(i==0) group.setSelected(option.getModel(), true);
            add(option);
        }
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        int i;
        
        for(i=0; i<nombres.length; i++) {
            if(nombres[i].equals(arg0.getActionCommand())){
                seleccion = i;
                break;
            }
        }
        //System.out.println(nombres[seleccion]);
    }
    
    int getSeleccion() {
        return seleccion;
    }
}
