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);
slView->setViewedComponent(slComp);

then when a button is clicked:

slView->setTopLeftPosition(300, 100);
slView->addToDesktop(ComponentPeer::windowHasTitleBar);

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

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

Thanks!

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!

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