Results 1 to 2 of 2
Thread: Custom Listeners
- 11-11-2012, 08:45 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 6
- Rep Power
- 0
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
Then whenever the MessageListener is referencedJava Code:public class IRCSocket { public IRCSocket() { //code } class Listener implements Runnable { @Override public void run() { //more code fireEvent(msg) //obviously a message was received } }
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.Java Code:public class foo implements MessageListener { @override MessageReceived(String msg) { //call parse method on msg System.out.println(msg); } }
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?
- 11-13-2012, 04:25 PM #2
Re: Custom 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.without having to manually add them to a list of listeners
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
-
Key Listeners
By wired-in=p in forum New To JavaReplies: 17Last Post: 09-17-2011, 08:27 PM -
two different Listeners
By Billaguana in forum New To JavaReplies: 8Last Post: 01-21-2011, 03:20 AM -
key listeners
By zjames in forum New To JavaReplies: 22Last Post: 11-24-2010, 05:58 AM -
Help with Listeners
By Psyclone in forum AWT / SwingReplies: 8Last Post: 02-09-2010, 07:21 PM -
Seriously need help on my listeners!!
By themburu in forum Java AppletsReplies: 4Last Post: 05-26-2008, 10:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks