Solved

addActionListener ....

Posted on 2004-09-25
6
149 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
Comment Utility
'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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 9

Accepted Solution

by:
OBCT earned 150 total points
Comment Utility
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
Comment Utility
Thanks
0
 
LVL 9

Expert Comment

by:OBCT
Comment Utility
:-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now