0

In different parts of my code, I will be running a similar variant of:

        _grid[4][4].addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            moveBeginningPieceLeft(_square[4][11]);
            refreshBoard();
        }

    });

The only thing that will change is coordinates of _grid. I tried doing something earlier where I made an actionlistener, but eclipse was forcing me to make the integers final. I'm wondering if it is easy to write an addListener method that uses the values of the grid that it is being called on(_grid is an array of JButtons and _square is an array of stacks). So if I called addListener on _grid[2][5], it would just take the coordinates [2][5] from _grid and place in the _square section of the actionlistener. So,

_grid[0][5].addListener();

is the same as

        _grid[0][5].addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            moveBeginningPieceLeft(_square[0][5]);
            refreshBoard();
        }

    });
mKorbel
  • 109,525
  • 20
  • 134
  • 319
dclark
  • 471
  • 2
  • 6
  • 9
  • 1
    Not unless the object it'self holds the coordinates. In your case no. You will have to rely on two index variables that are in scope for both _grid and _square. – Sterling Duchess Apr 02 '13 at 23:29

1 Answers1

1

I'm wondering if it is easy to write an addListener method that uses the values of the grid that it is being called on(_grid is an array of JButtons and _square is an array of stacks).

  • no there aren't

I tried doing something earlier where I made an actionlistener, but eclipse was forcing me to make the integers final.

  • declare required vasriable as local variable

  • examples about most easiest of ways by using put/getClientProperty, you can to multiplay this methods with another additional value

  • for better help sooner post an SSCCE, short, runnable, compilable, reduced and only about grid of JButtons

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319