I have a setListener() method that has the following inside:
for(int k = 0; k < buttons.length; k++)
{
buttons[k].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
g2.setColor(colors[k]);
}
});
}
I have a JButton array and a Color array and I was hoping I could add them quickly in one shot rather than manually adding an anonymous ActionListener 9 times (I have 9 components). It tells me I need to make k final for an inner class and when I tested it by removing the for loop and keeping k as a final integer, it works. Is there a medium such that I can achieve what I want more or less while respecting Java's syntax?
Any input would be greatly appreciated!
Thanks in advance!