Solved

addActionListener ....

Posted on 2004-09-25
6
153 Views
Last Modified: 2011-09-20
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]) ;
      }
0
Comment
Question by:meow00
  • 3
  • 3
6 Comments
 
LVL 9

Expert Comment

by:OBCT
ID: 12153733
'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.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12158061
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) {
          }


     }
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 150 total points
ID: 12158171
You can of course use a new class for the ActionListener:

public class MyActionListener implements ActionListener {
    public MyActionListener() {
    }

    public void actionPerformed(ActionEvent evt) {
       // Do your stuff
    }
}

that way you can write:

public class PlayGame {

   private MyActionListener myActionListener = new MyActionListener();


   ...
   mineButton[i].addActionListener( myActionListener );   // line 00
   ...
}
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 9

Accepted Solution

by:
OBCT earned 150 total points
ID: 12158274
Another way would be to have an anonymous inner class. This way wouldn't necessarily be any easier or harder; however it’s just another option.

I would personally write a method to return your actionListener like so...

private ActionListener getActionListener()
{
    return new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            // process actions here
        }
    };
}

This way you would have full access to all private members and methods within your class and remove the need for constructing an instance of a separate class.

At the end of the day, there’s heaps of ways you can do this, so just pick your favourite ;-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12177161
Thanks
0
 
LVL 9

Expert Comment

by:OBCT
ID: 12179465
:-)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question