Error accessing local variable in anonymous class

0

For some reason netbeans asks to change the variable i to FINAL, but if so, I will not be able to edit it.

The following is the error:

  

Error: local variable is accessed from within inner class; needs to be declared final

The idea is to create an interface of the old game, adding buttons in a layout with 3 columns and 3 lines.

At the time of adding eventHandlers to each button using the FOR loop, I can not because the method of class ActionEvent sends the variable to be FINAL. What could be wrong?

package TicTacToeProject;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * 
 * @author Igor
 */
public class PainelXD extends JPanel {
        private final JButton[] botoes;

    PainelXD(){

        setLayout(new GridLayout(3,3));
        botoes = new JButton[9];

        for (int i = 0; i <botoes.length; i++) {

            botoes[i] = new JButton("btn"+(i+1));

            botoes[i].addActionListener(new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent ae) {
                     botoes[i].setIcon(new ImageIcon(this.getClass().getResource("ticO.png"))); // erro aki local variable i is accessed from within inner class; needs to be declared final
                }


            });

            this.add(botoes[i]);

        }



    }

}

New code, bug fixed, but now it is giving ArrayOutOfBounds sinister and incomprehensible when using the method of class ActionListener in the buttons using cliclo FOR. Why are you giving this error?

public class PainelXD extends JPanel {
         final JButton[] botoes;

         private int lol;

    PainelXD(){

        setLayout(new GridLayout(3,3));
        botoes = new JButton[9];

        for (lol = 0; lol <botoes.length; lol++) {

            botoes[lol] = new JButton("btn"+(lol+1));
            //System.out.println();

            botoes[lol].addActionListener(new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent ae) {
                     botoes[lol].setIcon(new ImageIcon(this.getClass().getResource("ticO.png"))); // erro aki local variable i is accessed from within inner class; needs to be declared final
                }


            });

            this.add(botoes[lol]);

        }



    }

}
    
asked by anonymous 05.03.2017 / 04:11

1 answer

1

Try this:

botoes[i].addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent ae) {
        JButton btn = (JButton) ae.getSource();

        btn.setIcon(new ImageIcon(this.getClass().getResource("ticO.png")));
    }
});

Instead of using the button's external reference from the local loop index within the anonymous class to add an image (and how can it be seen here that is not possible), the above code does the same thing, only rescuing the button through its listener . The method getSource() returns the object from where the event was triggered, in this case, the JButton itself.

    
05.03.2017 / 05:14