Solved

addActionListener ....

Posted on 2004-09-25
6
162 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

617 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