I have something like the code below:
for(int i=0;i<10;i++){
button=new JButton(buttons[i]);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
setPage(i);
}
});
menu.add(button);
}
However, the variable i isn’t defined in the scope of the ActionListener class. How can I pass the variable?
In addition to Hovercraft’s answer, you should note that you’re not forced to use anonymous classes for your listeners. The code of Hovercraft’s answer is similar to the following one: