Hi All,

Is it possible to bind an array or a Vector with a event listener? For an example if we add an element to the Vector or remove from the Vector, can I use an event listener to track the changes?

Thanks a lot in advance.

Cheers..!!

How do you add/remove elements from the Vector?
Don't you have to do that programatically?
So... You know WHEN it is happening, why would you need a listener?
A listener "listens" for events that you don't know when they will occur.
(Like a user; pushing a button, checking a box, or selecting a radio button)

I think you will have to wrap the Vector (or extend the Vector class) in your own class that implements an addListener(...) method and overrides add() etc to broadcast the change event forwarding the change to the the Vector.

This is basically what javax.swing.DefaultListModel does to support JList objects - you may find that a javax.swing.DefaultListModel is all you need.

Hi all,

@hfx642
Actually I am using 2 classes who shares the Vector object. When the second class updates the Vector. I need to track the updates of the Vector from the first class.

// Has a Vector instance variable called vFirstClass;
FirstClass c1 = new FirstClass(); 

// This instantiation will be done in a method of FirstClass 
SecondClass c2 = new SecondClass(vFirstClass);

Thank you so much for the reply hfx642 :)

@JamesCherrill

I will try what you have suggested in my code. If you get any other solution from the above explanation I gave to hfx642, please be kind enough to post it too :) :)

Thank you so much JamesCherrill.

Cheers to all..!!!

Yes, that explanation clarifies it. DefaultListModel sounds ideal - it supports a Vector-like data model and was designed for the JList GUI component to track its changes, but the Listener design pattern it uses will work for any other class that wants to know about changes. I doubt you will find a (much) better solution.

Hi JamesCherrill,

Thanks a lot for the knowledge :)
I will try it as soon as possible and get back to you

Thanks again :) :)

Cheers..!!

This question has already been answered. Start a new discussion instead.