Get text from a JCheckBox array by adding the ChangeListener to it within a for

0

my problem is that inside the for where I start my JChekBox array I add the ChangeListener step, but inside this I do not know how to get the JChexBox index to get the text that is in it. example:

    for(int x = 0; x < cbConvertirDe.length; x++){
        cbConvertirDe[x] = new JCheckBox(elementos[x]);
        cbConvertirDe[x].setBounds(264, posicionY, 200, 23);
        cbConvertirDe[x].addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                etiquetaNumero.setText("NUMERO "+cbConvertirDe[].getText()); //dentro de los corchetes no se que poner, no puedo utilizar la 'x' del for 
            }
        });
        panel.add(cbConvertirDe[x]);

        cbConvertirA[x] = new JCheckBox(elementos[x]);
        cbConvertirA[x].setBounds(466, posicionY, 200, 23);
        panel.add(cbConvertirA[x]);

        grupo1.add(cbConvertirDe[x]);
        grupo2.add(cbConvertirA[x]);
        posicionY += 23;
    }'
    
asked by Alexis Rodriguez 19.11.2016 в 05:51
source

1 answer

0

I think I just solved my own question, the solution was not to add the ChangeLister to it in the same initialization, but I did a method that called it after the for which it was the following: '

public void agregarChangeListener(){
        for(int i = 0; i < cbConvertirDe.length; i++){
            cbConvertirDe[i].addChangeListener(new ChangeListener() {

                public void stateChanged(ChangeEvent e) {
                    int indice = 0;
                    for(int x = 0; x < cbConvertirDe.length; x++){
                        if(cbConvertirDe[x].isSelected()){
                            indice = x;
                        }
                    }
                    etiquetaNumero.setText("NUMERO "+cbConvertirDe[indice].getText());
                }
            });

        }
    }'
    
answered by 19.11.2016 в 06:07