• 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

ActionListener -- where to put anonymous inner class?

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to use an an anonymous inner class to make this work. I'm not really sure how they work. I'm sure my code is way off the mark. Please let me know where I'm going wrong. thanks :-)

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I need to use an an anonymous inner class to make this work.


I don't see how that's possible. As far as I know, an anonymous inner class just creates an object from a pre-existing class, and you can override one or more of the class methods as you create the object. So, as an alternative, you could define a subclass that extends the pre-existing class, which overrides one of the pre-existing class's methods, and then you can create an object of the extended class. Same end result.

See if this helps you with your understanding of anonymous inner classes in general:

http://www.sitepoint.com/forums/showpost.php?p=1892977&postcount=14

and more specific to your situation:

http://www.javaranch.com/campfire/StoryInner.jsp
[ October 06, 2006: Message edited by: sven studde ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have already created an anonymous inner class, it's in your method backgroundColoration() in class ButtonPanel.

The following lines in the constructor of class ButtonPanel are not going to compile:

Why not? Because class ButtonPanel does not implement interface ActionListener. You can't add "this" (the ButtonPanel object) as an action listener to the buttons.

Instead of this, you probably want to add the ActionListener that you've created in the method backgroundColoration() as the action listener for the buttons.

Note that at the moment, in class backgroundColoration() you are creating an instance of your anonymous inner class, but you're not doing anything with it. Also, you're not calling the method backgroundColoration() anywhere.
 
Jesse Crockett
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand what you mean. I have to do it this way (or whichever way to get an anonymous class to work. What do you mean by the latter portion of the quoted text?

Why not? Because class ButtonPanel does not implement interface ActionListener. You can't add "this" (the ButtonPanel object) as an action listener to the buttons.

Instead of this, you probably want to add the ActionListener that you've created in the method backgroundColoration() as the action listener for the buttons.

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try something like this:



Basically, need to to create an anonymous instance for each button. On the plus side, you can get rid of that ugly switch statement. On the minus side, there are some scoping issues (for lack of a better term) with anonymous inner classes. For example, your switch statement checks the source of the action event and sets the background color of the current object to some color. This works because the current object is a subclass of JPanel (presumably). However, with anonymous inner classes, effectively you're dealing with completely different objects so this won't work:


The "this" reference no longer points to the instance of your original class; it points to the instance of that anonymous inner class which doesn't have a setBackground method. That's why I created the setBackgroundColor method in the first example.

I'm assuming this is for homework so this next point won't help you, but I believe that the new NetBeans GUI editor (Matisse) does all of this anonymous ActionListener stuff for you.
[ October 06, 2006: Message edited by: Timothy Frey ]
 
Jesse Crockett
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
many thanks. i've got this program running. :-)
 
reply
    Bookmark Topic Watch Topic
  • New Topic