Trying to understand Viewport



Playing around and created a trivial application that puts an instance of MusicPane in a Viewport and displays it. MusicPane inherits from Component and calls setSize(1000, 1000) in its constructor. This code works as expected:

setSize (600, 400);
musicPane.setSize(1000, 1000); // why is this necessary?
viewport.setViewedComponent(&musicPane, false);
viewport.setViewPosition(500, 500);

The MusicPane shows up and everything works fine. What puzzles me is that if I fail to call musicPane.setSize() in MainComponent(), the Viewport doesn’t show anything at all; I just see the MainComponent. How is calling musicPane.setSize() here different from calling setSize() in MusicPane’s constructor?

(Juce 5.3, if it matters.)


You shouldn’t add musicPane as a child component of your MainComponent if you’re putting it in a Viewport (see the docs here). Remove the addAndMakeVisible(musicPane); line from your constructor.

(Also, if you surround your code blocks with three backticks ``` then it will be formatted correctly).


Ah. Makes sense. Thanks.