Results 1 to 5 of 5
Thread: EventListener for Vectors?
- 01-12-2011, 10:42 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
[Solved] EventListener for Vectors?
Hello,
I am currently working on a school project in which I am to create a working application of my choice. I've decided to create a chat-client/chat-server suite.
The setup is very easy: I run a chat-server on my computer at home, which handles all communication between the clients, as well as all specific commands (such as whois/pm's) and so on.
Now, my issue is as follows: My server creates a new serverThread for every accepted connection and places it into a Vector. The thread is then self-sufficient, so that if the socket is closed it'll automatically null itself and exit. However, this means that I have no possibility of tracking changes to the vector, and by extent no way of keeping track of connecting/disconnecting clients, which means I have no way of telling when to broadcast the users to the clients.
So, in short: Is there any event which is fired upon a variable change, and which I can apply to my vector? I can't find any inherited ones in the java documentation, but I'm sure there's some way to do it. Other solutions which give me the same functionality (easily managed variable and tracking) are obviously also acceptable.
With hopes of help,
TomLast edited by Tomtefaen; 01-14-2011 at 09:58 PM.
- 01-12-2011, 11:14 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
If I understand correctly,there isn't something in the standard java library but you might find a 3rd party library which has something like this. That being said it is pretty easy to either extend Vector or implement the List interface, using an Observer pattern to register listeners and fire said listeners whenever something is added/removed/etc...
- 01-12-2011, 11:43 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Hmm, that might make sense, yes. Did not know this option existed, though it is logical that there is one.
Now, as far as I understand it, I need to create a new Observable Vector class, right? Then my server needs to implement Observer. However, it seems like both Observable and Vector are classes and not interfaces, and I can't extend both. What am I doing wrong here? Google tell me there at least used to be an observable interface, but it does not exist in the documentation as far as I can tell.
Edit: Actually, the google results were talking about extending the observable interface and implementing the observable class. :(
I guess I could do nested class inheritance but I've always been told that multiple inheritance is nono, since you can inherit from 'grandparents' as well and then it gets really creepy...Last edited by Tomtefaen; 01-12-2011 at 11:47 PM.
- 01-12-2011, 11:56 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
See Observer Pattern. You don't have to use the Observer/Observable provided by java...these types of patterns are quite easy to write and implement.
- 01-13-2011, 12:02 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Alright, I'll see what I can make of it tomorrow - It seems like the object to be observed still needs to extend observable, if I were to follow that article.
Figured I'd expand on the way my program is build, in case I was being unclear on the issue:
I've got two programs, my client and my server.
The server program has a server class, which handles incoming connections and delegates them to fresh serverThreads (another class), which deals with I/O operations.
These serverThreads are stored in a vector declared in the server class,
Each client has a DefaultListModel which is supposed to contain all connected clients, hence I need to have a way of tracking changes to the serverthread variable, declared in server.class so I can broadcast the new client-list after it has changed.Java Code:public Vector<ServerThread> serverThread = new Vector<ServerThread>();
Last edited by Tomtefaen; 01-13-2011 at 12:16 AM.
Similar Threads
-
Netbeans, Editable JCombo and EventListener
By mine0926 in forum NetBeansReplies: 0Last Post: 07-10-2010, 03:55 AM -
Need Help Regarding Vectors
By xHoly in forum New To JavaReplies: 6Last Post: 05-23-2010, 06:56 AM -
JDialog isn't Showing till return from eventListener
By TimHuey in forum NetBeansReplies: 7Last Post: 09-12-2009, 07:06 PM -
have an eventlistener change values
By klmdb in forum New To JavaReplies: 1Last Post: 03-20-2009, 12:42 AM -
Vectors of Vectors or hash-somethings?
By mindwarp in forum New To JavaReplies: 3Last Post: 03-10-2008, 02:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks