Solved

addActionListener ....

Posted on 2004-09-25
6
159 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
configure dependency in POM for new database 3 34
iterator/ListIterator approach 17 39
throw exception 21 58
Java 8 to Java 6 8 22
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

830 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