• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it possible to put a change listener on a Hashtable ???  RSS feed

 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again.
Is it possible to put a change listener on a Hashtable?
My objective is to display the content of the Hashtable to the user everytime data has been added to or deleted from the Hashtable.
Could you please point me to links, documentation, tutorials, write-ups etc
I'm plowing thru these event-type listeners in Zukowski's and there seems to be a lot, but nothing specific to Hashtable?
TIA :-)
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, Hashtables don't support change listeners. You could subclass Hashtable and add change listener support in overridden methods.
 
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
Thanks for your suggestion.
What sort of a listener should be used?

Sorry for posting so many questions, but I'm getting there slowly, just a matter if whether I run out of resouce first... :-)
TIA :-)
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can make up your own listener if you want, of course. The closest thing in the official API would be java.beans.PropertyChangeListener, yes?
 
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THX for that :-)
I don't know about making up my own listener, I think you might be overestimating my Java, its sort of a weak, decaf. Yes, that's what I'll call my Java. "WeakDecaf"... :roll:
public class WeakDecaf extends Wannabe{
public void WeakDecaf(){
this.Terminate();
}
public Terminate() {
System.exit()
}
}
Something like that, but probably won't compile either! ;-)
 
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I've a little bit more guidance please, I'm really struggling here?
TIA :-)
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a Hashtable that notifies when data is added:

And then you might have something like
 
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your time and the code.
I'll try and understand it first.
 
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seeking explanation for this code. The instantiated Hashtable and this code are in the Model "controlApplet.java".

After over-riding the put() method, do I need to use that in the View.java applet?
Prior to subclassing Hashtable, View.java was able to insert into paramTable in controlApplet.java.
After introducing this code, I have following runtime error:

Before inserting ABC into paramTable!
java.lang.NullPointerException
at BABView.<init>(BABView.java:159)
at cbsModel$4.actionPerformed(cbsModel.java:540)

View.java failed at this point
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The stack trace says the exception is in the BABView constructor itself, so the only possibility (based on what you showed me) is that the paramTable parameter is null.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming the use of the Hashtable has been implemented correctly (using a Map reference), you could do this kind of thing quite easily.
Take a look at the java.lang.reflect.Proxy class. This allows you to intercept method calls on an interface, which could be used to implement some kind of listener.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could of course, simply wrap the Map, with another Map, that performs a similar thing internally
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!