I am using a for loop to create a grid of 60 JButtons. What I want to do is have the name of the actionListener increment each time the loops runs:
for (int y = 0; y < game.getHeight(); y++) {
for (int x = 0; x < game.getWidth(); x++) {
JButton square = new JButton();
square.setFont(new Font("Verdana", Font.PLAIN, 20));
ActionListener bh = new button_handler();
square.addActionListener(bh);
grid.add(square);
}
So I would like the name to increment (bh_1, bh_2, bh_3, etc).
Thanks!
If your goal is to be able to keep track of the
ActionListenersby name after creation, then you will want to use an array to do this. (If this is not why you want to name them differently, then I will need some more info.)Assume you have an
ActionListener[60]calledactionListenersand a counter calledbuttonCount.Now you will be able to access the
ActionListenersfrom the array.