954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Event Listener for a Vector or for an array

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..!!

niranga
Junior Poster
163 posts since Apr 2010
Reputation Points: 21
Solved Threads: 21
 

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)

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
 

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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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..!!!

niranga
Junior Poster
163 posts since Apr 2010
Reputation Points: 21
Solved Threads: 21
 

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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Hi JamesCherrill,

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

Thanks again :) :)

Cheers..!!

niranga
Junior Poster
163 posts since Apr 2010
Reputation Points: 21
Solved Threads: 21
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: