Detecting if the u/d scroll is appearing in [Table]ListBox?


Hello, Orangeys.

Tiny, tiny issue. I have a TableListBox which I’m dynamically sizing so it fills the remaining space to the right, and so that the last column on the right also fills its space.

This works very well, except if the TableListBox gets more elements than it can fit, in which case a up/down scroll appears… great, except that this makes my total width greater than the size of the table, so that I get an unneeded left/right scroll bar!

I have a simple hack, which is to always allow an extra 20 pixels for a possible up/down scroll bar when computing the width of the last column. This works fine, and really isn’t a big deal, but it’d be nice to actually get it right - either be able to detect the width of the ListBox without the up/down scroll bar, or be able to detect the existence of the up/down scroll bar at least.

As I said, tiny issue, but I’m a stickler for details…


Hmm, ideally Viewport would have a listener interface so we could know when things change. After a quick bit of digging though it looks like the viewport used by ListBoxes ias actually a custom one which calls ListBoxModel::listWasScrolled when the visible area changes:

juce_ListBox.cpp: line 183

[code] void visibleAreaChanged (const Rectangle&)
updateVisibleArea (true);

    if (owner.getModel() != nullptr)


Could you use this to check if the scrollbar is visible ListBox::getViewport()->isVerticalScrollBarShown() and adjust the size accordingly?

(I know I’ve used ListBox here but TableListBox is a ListBox so should work the same.)


Oh, very interesting! The viewport - hadn’t really looked into that…

…and it does get us a little further, but…

bool hasVertical = getViewport()->isVerticalScrollBarShown();
if (hasVertical) 
  newWidth -= getViewport()->getScrollBarThickness();
  DLOG(INFO) << "We never get here.  :-(";

Unfortunately, it seems as if isVerticalScrollBarShown() is always true, whether or not I can see a vertical scroll bar.


Looks like isVerticalScrollBarShown is just whether they should be shown or not. How about Viewport::getVerticalScrollBar()->isVisible()?


And Mr. David 96 receives the “solved it” prize!

Lazy me probably could have figured that last one out himself, eh…?