handleMessage confusion


#1

I’m afraid it’s not clear to me how to use the MessageListener class. I have several components for which I would like to pass messages between. Since the extra parameters in the Message class would be useful, my thought was to derive “CustomMessage” from Message, post one of those via PostMessage, then look for the custom type in the components handleMessage. However the components handleMessage never gets called, and I’m unsure if I need to register the component as a listener somehow.

I see that I can use ActionListener for the same purpose, which may be better suited for the task anyway… though I’m limited to a string message in this case. Just curious why what I did does not work.

Thanks :slight_smile:


#2

Well that sounds like exactly the right thing to do. Maybe you’ve just made a typo, or some kind of other misunderstanding? They do work, believe me, or nothing else would run at all!


#3

it sounds like you might be a bit confused about how MessageListeners actually work. I know I was at first.

The main confusion likely comes from the fact that it’s called MessageListener, which sounds similar to ActionListener, ChangeListener etc…, yet you use it in a different way.
[TBH, I really think it would be sensible to change the name of the class to something more intuitive, but it’s been there so long it wouldn’t be practical!]

It’s not the same setup as with those (where there are broadcasters and listeners). In the case of a MessageListener, it only listens to messages that are posted from itself. Its a system that has potential to sound pointless, but it’s not! It’s basically an asynchronous data handling system. You make a message, post it, and then you can respond to it when the message thread is ready.

So, if you want to use it to ‘communicate’, you’d use your message handling function to determine the target from the message (e.g. from a pointer), and then call an appropriate function on it (based on other information in your message object). ActionListener, ChangeListener etc… are simply specific types of target for implementations of this, doing such things for you. I would be surprised if you really need to actually use the MessageListener class, unless what you intend to communicate is actually very specialised.


#4

Sorry, I misread the bit about trying to pick up the message in a component… What haydxn said is right, of course!