Use non-local variables in actionlistener

I want to use a variable in an action. this is my code:

public class Face {

public Face(){

    Btn1 =  new JToggleButton();
    Btn2 =  new JToggleButton();
    Btn3 =  new JToggleButton();
    Btn4 =  new JToggleButton();
    Btn5 =  new JToggleButton();
    Btn6 =  new JToggleButton();

}


public Start6(){

    JToggleButton[] Btn = new javax.swing.JToggleButton[5]; // An array for saving address of all buttons

    Btn[0] = Btn1 ;
    Btn[1] = Btn2 ;
    Btn[2] = Btn3 ;
    Btn[3] = Btn4 ;
    Btn[4] = Btn5 ;
    Btn[5] = Btn6 ;

   for (int i = 0 ; i < 36 ; i++) {
        Btn[i].addActionListener(new ActionListener() {     
        @Override
        public void actionPerformed(ActionEvent e) {
        Btn[i].setSize(150, 150);
        ImageIcon BtnImage = new ImageIcon(getClass().getResource("/GameImages/" + /* a picture name*/ + ".jpg" ));
        Image img = BtnImage.getImage();
        Btn[i].setIcon( new ImageIcon(img.getScaledInstance(225,160 ,Image.SCALE_FAST)));
        X6P.add(Btn[i]);
        }
    });
    }


   }
  public javax.swing.JToggleButton Btn1;
  public javax.swing.JToggleButton Btn2;
  public javax.swing.JToggleButton Btn3;
  public javax.swing.JToggleButton Btn4;
  public javax.swing.JToggleButton Btn5;
  }

      

when i want to use from Btn array in Btn array action manifest (same arrays) i get this error:

local variables referenced by the inner class must be final or effectively final I want the icon to be displayed when the button is clicked ...

+3


source to share


1 answer


The error means what it says: your array variable is Btn

not final

, so it cannot be accessed to the inner class. Crafting final

fixes the problem:

final JToggleButton[] Btn = new ...

      

Another solution would be to move the array to a class:

public javax.swing.JToggleButton Btn1;
public javax.swing.JToggleButton Btn2;
public javax.swing.JToggleButton Btn3;
public javax.swing.JToggleButton Btn4;
public javax.swing.JToggleButton Btn5;
private javax.swing.JToggleButton[] Btn = new javax.swing.JToggleButton[] {
    Btn1, Btn2, Btn3, Btn4, Btn5
};

      

It will do the same and make your code shorter.



I want the icon to be displayed when the button is clicked

You don't need to use an array at all: instead of a link Btn[i]

, a link e.getSource()

. It's set to the button the button was clicked on - all you have to do is throw it:

javax.swing.JToggleButton sender = (javax.swing.JToggleButton)e.getSource();

      

Create one action listener outside the loop and set it on all of your buttons. This would eliminate the array entirely:

// The same listener instance can handle all buttons
ActionListener actListener = new ActionListener() {     
    @Override
    public void actionPerformed(ActionEvent e) {
        javax.swing.JToggleButton senderButton = (javax.swing.JToggleButton)e.getSource();
        ... // Do anything you want with senderButton here
    }
};
// Add the same listener to all buttons
for (int i = 0 ; i < 36 ; i++) {
    Btn[i].addActionListener(actListener);
}

      

+1


source







All Articles