aspose file tools*
The moose likes Beginning Java and the fly likes Two ActionListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Two ActionListener" Watch "Two ActionListener" New topic
Author

Two ActionListener

Niyas Ahmed
Greenhorn

Joined: Jun 03, 2004
Posts: 1
Hi,

I am learning the Java thru Head first Java book. Now I am in AWT. I have one doubt in the below pgm.

I have two buttons, each have to do a separate thing. We can't (implement) use the actionPerformed() twice in the same class. So for that, i have two classes for two buttons.

Note: I know there is one better way than this to solve it. That is Inner Class

I just want to do it other than Inner Class, just for exercise.

How could i fix the below pgm. How o give each of the listener classes a reference to the main class.(I think that is the problem)



Error
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
You have two choices. You can leave your ActionPerformed methods in the button classes and use e.getSource() instead of b1/b2.
i.e.or you can put the ActionPerformed method in your main class and use e.getSource() to check which button was clicked.i.e.


Joanne
Niyas Ahmed Sheikh
Ranch Hand

Joined: Jun 15, 2005
Posts: 129
Hi,
Thanks for your reply Joanne Neal. It is working.

Method 1:


Method 2:


Questions:

You can leave your ActionPerformed methods in the button classes and use e.getSource() instead of b1/b2.


How it is possible to use only 'b'. I have two button b1 and b2. I just confused with this. however in the code given by you I used b1 and b2(just see the Method 1 and Method 2).

Then what is getSource() method is used for. I tried to put System.out.println(e.getSource()) to know what it's having. But it is not printing anything.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
Originally posted by Niyas Ahmed Sheikh:
How it is possible to use only 'b'. I have two button b1 and b2. I just confused with this. however in the code given by you I used b1 and b2(just see the Method 1 and Method 2).

Then what is getSource() method is used for. I tried to put System.out.println(e.getSource()) to know what it's having. But it is not printing anything.


e.getSource() returns a reference to the object that triggered the Action. So in this case it will be a reference to the button that you clicked. b1 and b2 also reference these buttons, so e.getSource() will be equal to either b1 or b2.

In method 2, the Awtex1 class should implement actionListener and you should pass an instance of the class to the button's addListener method.
i.e.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
Originally posted by Niyas Ahmed Sheikh:

Method 2:


Just noticed also that this method should be
Niyas Ahmed Sheikh
Ranch Hand

Joined: Jun 15, 2005
Posts: 129
Hi,

I am getting the strange output:



The above program is compiling & running without any problem. I am wondering how "new FirstButton()" and "new SecondButton()" is working without having that class.


Awtex1.java:25: addActionListener(java.awt.event.ActionListener) in java.awt.But
ton cannot be applied to (Awtex1)
b1.addActionListener(this);
^
Awtex1.java:26: addActionListener(java.awt.event.ActionListener) in java.awt.But
ton cannot be applied to (Awtex1)
b2.addActionListener(this);
^
2 errors
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
Awtex1 needs to implement ActionListener
Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200
Hi,
Once again thank you very much for your help!!

I want to see the flow of the program step by step, like in Visual Basic 6.0 we having checkpoint.

For java which IDE is best with the above option and other advanced options
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
Originally posted by Mark Henryson:
Hi,
Once again thank you very much for your help!!

I want to see the flow of the program step by step, like in Visual Basic 6.0 we having checkpoint.

For java which IDE is best with the above option and other advanced options


I think most IDEs offer a debugging option that allows you to step through the code and add breakpoints. As to which is best, it's a matter of taste. Try a few out and see which you like.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Two ActionListener