Painting a ListBox/ListBoxModel derived class?

I created a class (in Projucer) that derives from ListBox and ListBoxModel, and sets itself as the model. Then I dropped a Generic Component on a view component, and set its class to my derived class. When I generate my projects, I get a paint() function in the class, but had to add the paintListBoxItem() function myself. I did so, and added code there to draw the appropriate text for the given row. I also populated a list of Strings to be shown in the listbox, and implemented getNumRows() to return the number of strings added.

My question is: do I have to write code to loop and call paintListBoxItem() in my paint() function? I ask because my paintListBoxItem() function is never called. And looking at the ListBox implementation of the paint() function, I don’t see anything there that performs such a loop. I can’t seem to find out how that function is supposed to get called, or if I have to do it myself.

Thanks for any advice!

It works using a default RowComponent, which calls your paint in the model:

But as soon as you implement a custom component to be displayed by overriding ListBoxModel::refreshComponentForRow(), this won’t be called any more.

Another thing, when the content changed (i.e. the getNumRows() returned 0 before), you have to call ListBox::updateContent() to trigger the update.

Hope that helps, I can’t tell if this was the actual problem without knowing more about your implementation.

N.B. from an architectural point of view, it is advisable to keep the model and the view separated, but at the moment I don’t see a technical problem with that.

Thanks. I changed my custom class to derive from Component and ListBoxModel (instead of ListBox and ListBoxModel), and added the ListBox as a member of that class instead, and now it works fine. Not sure why that works when the other way didn’t, but the goal is to get it working, not marry to an idea, right? :slight_smile: Thanks for the help!

Interesting. I would have thought there might be a name clash of some overrides. But I just realised, that FileListComponent does exactly what you tried: it inherits ListBox and ListBoxModel. So there seems to be something else that you need to take care of.

But glad that it works now.

I guess you probably overrode a virtual method that ListBox uses, without calling the super-class, which could easily break something in the way it works,

The paint() function was overridden automatically by Projucer, and it simply painted the background. Calling ListBox::paint(g) did’t help. The only functions I added were the required ListBoxModel overrides for getNumRows() (which was called, and returned the correct value), and paintListBoxItem() (which was never called). That’s it. No idea why it didn’t work.