Results 1 to 2 of 2
  1. #1
    Jdsfighter is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Custom Listeners

    Hey everyone, I'm having some issues trying to wrap my head around custom even listeners. Here's my scenario.

    I'm building an IRC Library, and one of my objects is a Socket, this socket can be placed into several states, but it is able to connect, close, listen, and send.

    I'm trying to figure out how I can make it throw an event when a message is received.


    I was thinking something along the lines of

    Java Code:
    public class IRCSocket {
    	public IRCSocket() {
            //code
    	}
    	
    	class Listener implements Runnable {
    		@Override
    		public void run() {
    		//more code
                    fireEvent(msg) //obviously a message was received
    		}
    	}
    Then whenever the MessageListener is referenced

    Java Code:
    public class foo implements MessageListener {
         @override
         MessageReceived(String msg) {
               //call parse method on msg
               System.out.println(msg);
         }
    }
    In the above scenario, foo would have referenced the IRCSocket as well as the MessageListener, and from foo, messages could be sent, received, parsed, etc.

    How would I go about making it possible so that every class that implements MessageListener is instantly able to receive MessageReceived events without having to manually add them to a list of listeners?

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Custom Listeners

    without having to manually add them to a list of listeners
    I'm not sure you can. When using event listeners elsewhere in java, you have to add the listener to the item you want to use it with manually. You are in essence "registering" the component with the listener system.

    An example would be something like a MouseMotionListener. You can subclass one, do all kinds of fancy things with it, but it will do nothing until you add that listener to the JComponent which will use it.

Similar Threads

  1. Key Listeners
    By wired-in=p in forum New To Java
    Replies: 17
    Last Post: 09-17-2011, 08:27 PM
  2. two different Listeners
    By Billaguana in forum New To Java
    Replies: 8
    Last Post: 01-21-2011, 03:20 AM
  3. key listeners
    By zjames in forum New To Java
    Replies: 22
    Last Post: 11-24-2010, 05:58 AM
  4. Help with Listeners
    By Psyclone in forum AWT / Swing
    Replies: 8
    Last Post: 02-09-2010, 07:21 PM
  5. Seriously need help on my listeners!!
    By themburu in forum Java Applets
    Replies: 4
    Last Post: 05-26-2008, 10:41 AM

Posting Permissions

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