How to Use a Viewport


Seems simple enough:

“To use a Viewport, just create one and set the component that goes inside it using the setViewedComponent() method. When the child component changes size, the Viewport will adjust its scrollbars accordingly.”

So, I started with an existing component (slComp) that appears as a window when the user clicks a button. The user can add items to that component and increase the vertical size. That worked and now I need a vertical scrollbar, but I must not be using Viewport correctly.

slComp = new SpeakerLayoutComponent(); // a custom component with rows of text boxes for specifying a speaker layout
slView = new Viewport();
slView->setSize(800, 600);

then when a button is clicked:

slView->setTopLeftPosition(300, 100);

// i used these lines before i needed a Viewport and it worked fine
//slComp->setTopLeftPosition(300, 100);

Nothing shows now. There must be another way to show the viewport. What am I missing or doing wrong?



ListBox uses a Viewport to scroll the cells, if you can’t come up with anything, then step through the juce demo in the debugger to see how the ListBox uses it.


Did you actually make the view visible? It won’t be visible by default.


I had a similar problem, and yes indeed it was me not calling addAndMakeVisible on my viewport object.

But afterwards I call removeAllChildren() and then call addAndMakeVisible(viewPortObj). And now, my child component is not visible. (yes, my addAndMakeVisible is followed by setViewedComponent and setViewPosition). I am trying to find where I am going wrong. Thanks for the help!

Size of GUI component and viewport use with relative widths?

Well, I manually called resize (is that Ok?) from my function before calling addAndMakeVisible(viewPortObj). Now it is good.