Link to home
Start Free TrialLog in
Avatar of meow00
meow00

asked on

addActionListener ....

Hi Experts,

    I have a question about the following code :

  at line 00, does "this" refer to a PlayGame object or the mineButton[i] ?
  so .... must an "addActionListener" work with an actionPerfomed() method ? thanks !
-------------------------

public PlayGame(){
      System.out.print(ROW_NUM + "  " + COL_NUM) ;
      JPanel minePanel = new JPanel() ;
      minePanel.setLayout(new GridLayout(ROW_NUM,COL_NUM)) ;
      for(int i=0 ; i < ROW_NUM*COL_NUM; i++){ mineButton[i] = new Mine(i) ;
                                  mineButton[i].addActionListener(this);   // line 00
                                  minePanel.add(mineButton[i]) ;
      }
Avatar of OBCT
OBCT

'this' refers to the PlayGame object.
So when calling addActionListener(this), your basically saying that the current ActionListener is implemented in the PlayGame object.
Therefore you'll need to make sure you do implement ActionListener for everything to work ok.
Avatar of zzynx
this always refers to the current instance of the class it is in, in this case the PlayGame class.
So you add an ActionListener to the mineButton[i].
And that ActionListener is an instance of PlayGame.
To be able to do that, your PlayGame class must implement the ActionListener interface.
That's rather easy: it should implement the actionPerformed function:


     public class PlayGame implements ActionListener {

         
          public void actionPerformed(ActionEvent evt) {
          }


     }
SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks
:-)