I've seen some posts on this before, but I haven't been able to find one regarding actionListeners. I am trying to create tic-tac-toe using an array of JButtons. How do I add an action listener to them whilst using a for loop temporary variable if at all possible? Thanks for your help in advance.
JButton jb [] = new JButton[9];
int checkB [] = new int[9];
public SomethingSimple(){
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3, 3, 5, 5));
p1.setBackground(Color.red);
for (int i = 0; i < jb.length; i++){
checkB[i] = 0;
}
for (int i = 0; i < jb.length; i++){
jb[i] = new JButton("");
p1.add(jb[i]);
jb[i].addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
jb[i].setText("O");
}
});
}
add(p1);
}
Thanks everyone for your help, you gave me some solutions!