File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Attach an event to a hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Attach an event to a hashmap" Watch "Attach an event to a hashmap" New topic
Author

Attach an event to a hashmap

Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
Hi is here any way how i could attach an event to a hashmap. For example I want a method called "Somemethod" to get trriggered when i put smthing in the container . Any suggestions on how i could accomplish tht ??
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

One way is to write a subclass of HashMap which overrides the add() method -- or is it called "put", I can't remember offhand -- to do that.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30753
    
156

You could put the map in a class so you have to call that class to update the map and have that helper/wrapper method call the method you want called. Not sure if there is a better way.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
Jeanne Boyarsky wrote:You could put the map in a class so you have to call that class to update the map and have that helper/wrapper method call the method you want called. Not sure if there is a better way.

Yeah thats what i had in mind. cusrious if there was some other way
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

i dont think there's binding or trigger like feature in java, so the only way i can think of is already mentioned by Jeanne and Paul.


Mohamed Sanaulla | My Blog
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

In addition to what Paul said, I would suggest adding a java.beans.PropertyChangeSupport member in the subclassed HashMap, and methods for add/removePropertyChangeListener(...) [ edit: and getPropertyChangeListeners(...) ] that forward to the PropertyChangeSupport. Then, in methods of interest you could override to call the super implementation (i.e. HashMap implementation) and (maybe conditionally) fire a propertyChange. That would notify all registered PropertyChangeListeners.

If the notification has to go out to any objects of Swing classes, you might want to use a javax.swing.event.SwingPropertyChangeSupport, which invokes the listeners' propertyChange(...) on the EDT.


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

I'm not sure if PropertyChangeListener is the right choice. That requires the use of property names (Strings), where this solution would need type K (or the more general Object) for the key. Perhaps a custom listener / event pair is better:
I once created such a framework (listeners, events, wrapper classes around Maps, Collections, Lists, Sets, etc) but deleted it because I never used it.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Consider Paul's rocket mass heater.
 
subject: Attach an event to a hashmap