use this site a lot, but publish it first. My program creates several buttons depending on the number of entries in the file. For instance. 5 entries, 5 buttons.
Buttons are created, but I have a problem with the action listener.
If you add an action performer in a loop, each button does the same; but if I add an action listener outside the loop, it just adds an action listener to the last button.
Any ideas?
Here is what I have in the code (I just added a for loop to save space):
int j=0;
for(int i=0; i<namesA.size(); i++)
{
b = new JButton(""+namesA.get(i)+"");
conPanel.add(b);
conFrame.add(conPanel);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae2){
System.out.println(namesA.get(j));
}
}});
j++;
}
Significant score
source
share