How to focus on an area on a screen with JUCE UI


Hi all,
I have a screen that has a ScreenComponent is inside a ViewportComponent to make is can be scrolled. I draw some thing on ScreenComponent and the UI has a vertical scroll bar on the right side. How to set UI focuses to some where on the screen?





I got it. Thank you so much @Rail_Jon_Rogut


@Rail_Jon_Rogut, do you know how to get current position of vertical scroll bar inside a viewport?
I mean how to do if I want an icon moving follow the vertical scroll bar :slight_smile:

 double dPos = yourViewport.getVerticalScrollBar().getCurrentRange().getStart();



And if myViewport is inside a component, how to catch it’s scrollbar moving even @Rail_Jon_Rogut?


If you look at the ViewPort class source…

class JUCE_API  Viewport  : public Component,
                            private ComponentListener,
                            private ScrollBar::Listener

Notice that it’s derived from ScrollBar::Listener and has a method:

void Viewport::scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRangeStart)
    auto newRangeStartInt = roundToInt (newRangeStart);

    if (scrollBarThatHasMoved == horizontalScrollBar.get())
        setViewPosition (newRangeStartInt, getViewPositionY());
    else if (scrollBarThatHasMoved == verticalScrollBar.get())
        setViewPosition (getViewPositionX(), newRangeStartInt);

You can derive a class from ViewPort and override scrollBarMoved() which calls the base class and also does what you need.



Yes @Rail_Jon_Rogut, and I found that we can also use void visibleAreaChanged(const Rectangle<int>& newVisibleArea) override;
Thank you so much :slight_smile: