Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

addActionListener ....

Posted on 2004-09-25
6
Medium Priority
?
164 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 600 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 9

Accepted Solution

by:
OBCT earned 600 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

670 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