• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Two ActionListener

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Niyas Ahmed Sheikh:

Method 2:



Just noticed also that this method should be
 
Niyas Ahmed Sheikh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awtex1 needs to implement ActionListener
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic