I’m having a hard time finding an example for making a ListBox component that doesn’t use headers. My end goal is to have a simple single column list with ToggleButton rows. I think I’m good as far as the row items go, but I can’t find in the API docs any way of adding anything to a ListBox object. I have a ListBoxModel-derived class set up, but unsure where to go from there. If anything, I think I’m overdoing this when there could be a simple solution that I’m just not finding anywhere.
I’ll help bump it. I’m trying to piece this together myself at the moment. Some old forum posts mention a DragAndDropDemo.cpp but I don’t think it exists anymore (or at least I can’t find it). If I figure anything out I’ll let you know.
Think I figured it out! The FontsDemo was very helpful.
Here’s the simplest form of a listbox that I could get displaying text. Doesn’t attempt to pull data from anywhere, just generates text directly.
I’d wager that to use a custom component instead of just drawing text, you’d place it inside the painListBoxItem() function.
class BasicListBox : public ListBoxModel, public Component
{
public:
BasicListBox()
{
// In your constructor, you should add any child components, and
// initialise any special settings that your component needs.
addAndMakeVisible(listBox);
listBox.setRowHeight (30);
listBox.setModel (this); // Tell the listbox where to get its data model
listBox.setColour (ListBox::textColourId, Colours::black);
listBox.setColour (ListBox::backgroundColourId, Colours::white);
setSize (400,600);
}
~BasicListBox()
{
}
void paint (Graphics& g) override
{
/* This demo code just fills the component's background and
draws some placeholder text to get you started.
You should replace everything in this method with your own
drawing code..
*/
g.fillAll (Colours::lightgrey); // clear the background
}
void resized() override
{
// This method is where you should set the bounds of any child
// components that your component contains..
listBox.setBounds(0, 0, getParentWidth(), getParentHeight());
}
// The following methods implement the ListBoxModel virtual methods:
int getNumRows() override
{
return 12; //you should probably derive this from whatever source data you end up using
}
void paintListBoxItem (int rowNumber, Graphics& g,
int width, int height, bool rowIsSelected) override
{
if (rowIsSelected)
g.fillAll (Colours::lightblue);
g.setColour (Colours::black);
g.setFont (height * 0.7f);
g.drawText ("Row Number " + String (rowNumber + 1), 5, 0, width, height,
Justification::centredLeft, true);
}
void selectedRowsChanged (int /*lastRowselected*/) override
{
//do stuff when selection changes
}
private:
ListBox listBox;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BasicListBox)
};