Listeners and nested components


Suppose I have components A,B,C where B is a child of A and C is a child of B (A -> B -> C).

What is it best way to let A know that a child button of C has been pressed?


If a button performs some kind of action, it should do so by calling a suitable function in your data model classes, not by passing the event on to another component. Always try to separate your model from your UI.

I agree with Jules on the overall strategy!

If you need to, you can make A a juce::Button::Listener and call C.addListener(A) (don’t forget to removeListener at the end!)

Me, I like to rearrange my GUI a lot, or to allow the user to do it, so my buttons are actually self-contained and work anywhere they are placed. At the same time, I want almost no logic at all in my buttons!

So my buttons simply have an address and a data source. For example, my “Mute” button just toggles the value of audio.player.mute and it will work anywhere as long as someone attaches it to a data source - but every container attaches its kids to its datasource so I only have to attach one thing to my data source when I open a new file. The player listens to its data and turns off the sound when the button is pressed and everything is persistent because it’s persistent data!

So there’s no logic in my buttons, just a pointer to a data source and an address within that source. This is a pretty complex app I have here so this solution might not be for you but it certainly makes for flexibility in medium- to large-sized apps.