TabbedComponent and MessageListener


#1

I wanted to make TabbedComponent object post a message when its current tab is changed and to make another component handle that message; therefore I added

[code]void TabbedComponent::currentTabChanged(const int newTabIndex, const juce::String &newTabName)
{

Message * msg = new Message();
msg->intParameter1 = 16000;
postMessage(msg);

}
[/code]

and

void TestComponent::handleMessage(juce::Message &message)
{
if(message.intParameter1 = 16000)
label2->setText(L"00",0);
}

and the definition of TestComponent begins with

class TestComponent : public Component, public SliderListener, public ButtonListener { ....

The TabbedComponent posts message but TestComponent doesn’t handle it. How can I make TestComponent handle the message posted by TabbedComponent?


#2

How do you expect the TestComponent might receive the message? When you’re posting it (from a different component), there’s nowhere specifying a target, so it’s no wonder your handler isn’t getting called.

That’s not any fault of your own though, you’re just misunderstanding how the message system works; for that, i point you to my explanation in this thread.


#3

So how can I make different components communicate with each other? Is there any example to do this?


#4

By the way, I want to thank you for your Juce documentation haydxn, I learned so many things from your pdf documentation.


#5

There are a bunch of ways you could do it. You could, for example, make use of one of the broadcaster/listener combos. The ChangeBroadcaster is suitable in cases where you have one thing which might change often (e.g. an adjustable control of some kind), as it merges notifications that would be sent close together. The ActionBroadcaster is useful when you have something which may do several different things, as it sends a String which you can assess to determine your response.

If the source of your notification is only really doing one type of thing, and you need it to be sent asynchronously, an easy enough one to use is AsyncUpdater. You call triggerAsyncUpdate, and eventually handleAsyncUpdate is called; this is, you’ll notice, the same situation as with MessageListener, but what it does is get rid of the need to manufacture (and interpret) any message objects.

In your handler function, you’d simply communicate notification of the event to your target component; this would mean that you would have to store a pointer to it somewhere. That means registering your target component with your broadcasting component - you should notice that this is exactly what the Change/Action systems do for you.

Of course, you may not even need to respond asynchronously - you may be okay simply sending a notification directly from within currentTabChanged… either way, you need to register your target somehow. You may decide that it is enough to simply give your tabbedcomponent a pointer to the target in its constructor. It’s up to you!


#6

Thank you very much, I should apply the solutions you explained. Meanwhile, it would be very excellent having a messaging system between all the components.