2

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!

2
  • 3
    I get the question, and this can probably be done with reflection, but why would you want to change the name of the ActionListener variable here? That "name" is just a local reference and won't survive the scope of the loop iteration anyways, unless there's missing code here. Commented Jan 25, 2011 at 15:43
  • I don't think he wants to save the "name", since he's not setting the name at all. I think it's more that he wants references to the variables for later. Commented Jan 25, 2011 at 15:51

3 Answers 3

3

If your goal is to be able to keep track of the ActionListeners by 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] called actionListeners and a counter called buttonCount.

JButton square = new JButton();
square.setFont(new Font("Verdana", Font.PLAIN, 20));
ActionListener bh = new button_handler();
actionListeners[buttonCount++] = bh; // store the handler in an array for later
square.addActionListener(bh);
grid.add(square);

Now you will be able to access the ActionListeners from the array.

Sign up to request clarification or add additional context in comments.

Comments

0

I'd recommend using a list:

LinkedList<ActionListener> actionListeners = new LinkedList<ActionListener>();

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();

        // can access this action listener via actionListeners.get(indexNumber).
        actionListeners.add(bh);

        square.addActionListener(bh);
        grid.add(square);
    }

Comments

0

Store them in a two-dimensional array.

You could store these in any array, but I think storing them in a two-dimensional array makes sense since you have a grid of buttons and a grid of handlers. I also think it's a good idea to keep the reference to the buttons and the handlers. This will allow you to tweak the buttons as well as the button handlers at any later time.

Here is an example of how to do this:

JButton[][] squares = new JButton[game.getWidth()][game.getHeight()];
ActionListener[][] bhs = new ActionListener[game.getWidth()][game.getHeight()];
for (int y = 0; y < game.getHeight(); y++) {
  for (int x = 0; x > game.getWidth(); x++) {
    squares[x][y] = new JButton();
    squares[x][y].setFont(new Font("Verdana", Font.PLAIN, 20));
    ActionListener bhs[x][y] = new button_handler();
    squares[x][y].addActionListener(bhs[x][y]);
    grid.add(squares[x][y]);

  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.