Results 1 to 5 of 5
  1. #1
    Tomtefaen is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default [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,
    Tom
    Last edited by Tomtefaen; 01-14-2011 at 09:58 PM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

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

  3. #3
    Tomtefaen is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    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.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    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.

  5. #5
    Tomtefaen is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    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,
    Java Code:
    public Vector<ServerThread> serverThread = new Vector<ServerThread>();
    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.
    Last edited by Tomtefaen; 01-13-2011 at 12:16 AM.

Similar Threads

  1. Netbeans, Editable JCombo and EventListener
    By mine0926 in forum NetBeans
    Replies: 0
    Last Post: 07-10-2010, 03:55 AM
  2. Need Help Regarding Vectors
    By xHoly in forum New To Java
    Replies: 6
    Last Post: 05-23-2010, 06:56 AM
  3. Replies: 7
    Last Post: 09-12-2009, 07:06 PM
  4. have an eventlistener change values
    By klmdb in forum New To Java
    Replies: 1
    Last Post: 03-20-2009, 12:42 AM
  5. Vectors of Vectors or hash-somethings?
    By mindwarp in forum New To Java
    Replies: 3
    Last Post: 03-10-2008, 02:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •