Error accessing local variable in anonymous class

Asked

Viewed 514 times

0

For some reason netbeans asks to transform the variable i in the end, but if so, I cannot edit it.

Follow the error:

Error: local variable i is accessed from Within Inner class; needs to be declared final

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

When it’s time to add eventHandlers for each button using the FOR cycle, I cannot, as the class method ActionEvent has the variable be FINAL. What may 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 giving ArrayOutOfBounds sinister and incomprehensible when using the class method ActionListener on the buttons using click FOR. Why is 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]);

        }



    }

}

1 answer

1


Try it this way:

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 external reference of the button from the local loop entry within the anonymous class to add an image (and how it can be seen here this is not possible), the above code does the same thing, only rescuing the button through your Listener. The method getSource() returns the object from which the event was triggered, in this case, the actual JButton.

  • yes, your solution worked. but n understood absolutely nothing of your solution. : DDD

  • @Igoraugusto I edited with a brief explanation.

  • know q n it’s cool to replicate with a "thank you". but thank you! xd

  • @Igoraugusto dispose =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.