This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

those crazy listeners!  RSS feed

 
Micah Pezdirtz
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a class MyPanel that extends jpanel but implements ActionListener, so i can just add itself to components i throw into MyPanel, and it worked fine but here is where my problem is :
a certain button adds another custom component(that also has a button), so in the actionPerformed method im trying to add an actionListener(itself) to it, but the compiler doesnt like this at all and i get an error "cannot find symbol: method addActionListener(MyPanel)"
is it that you cant add an actionListener from within the actionListener you want to add? whats going on here?
earlier in the code(outside of the actionListener) i can call button.addActionListener(this) just fine, but i cant figure out why this is wrong here.
thanks for any help!
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The listener (which is a regular class, even if it doesn't look that way) does not have a method addActionListener. You need to invoke it with the object that you want it attached to, e.g. myPanel.addActionListener.
 
Micah Pezdirtz
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yea i found out my problem didnt have to do with that, it was that the method i was using to get the JButton from the other component was returning the jbutton, casted as a Component(and apparently that is what has no addActionListener() method. the method returning the component(which happened to be the jbutton) was supposed to also be usful for returning other types of components from out of an arraylist, which is why they were being casted as Components. turns out i only really need to be able to get the JButton, and never any other components....so i just changed it so its not casting as a Component... silly mistake...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!