The Red Penguin

Implement a play button and add a listener


Thursday 6 August 2020

1. We need to initialise a new function in PlaylistComponent.h
    juce::Component* refreshComponentForCell(int rowNumber,
int columnID,
bool isRowSelected,
juce::Component* existingComponentToUpdate)
override;

2. And initialise this in PlaylistComponent.cpp:
juce::Component* PlaylistComponent::refreshComponentForCell(int rowNumber,
int columnID,
bool isRowSelected,
juce::Component* existingComponentToUpdate)
{
return existingComponentToUpdate;
}

3. We need to add a new column for the play buttons, so we need to add this to show the header:
tableComponent.getHeader().addColumn("", 2, 200);

4. Add more code to the new function:
Component* PlaylistComponent::refreshComponentForCell(int rowNumber,
int columnID,
bool isRowSelected,
Component* existingComponentToUpdate)
{
if (columnID == 2)
{
if (existingComponentToUpdate == nullptr)
{
existingComponentToUpdate = new juce::TextButton{ "play" };
}
}
return existingComponentToUpdate;
The play button column has a column ID of 2 so we need to do a test on this column. If the existing component is nullptr (we haven't created it yet) then we have to create it.

5. Now we need to hook up a listener to this button so we can receive events from it.

Go to PlaylistComponent.h and add an extra class inheritance:
class PlaylistComponent  : public juce::Component, 
public juce::TableListBoxModel,
public juce::Button::Listener
(we have this in DeckGUI.h as well)

6. This requires us to implement one function so in public in PlaylistComponent.h we add:
void buttonClicked(juce::Button* button) override;


7. We need to implement the new function in PlaylistComponent.cpp so at the bottom of the .cpp file add:
void PlaylistComponent::buttonClicked(juce::Button* button)
{
(DBG "click");
}
which for now will just show a button click to the console.

8. We need to create a TextButton pointer in juce::Component* PlaylistComponent::refreshComponentForCell so we add a line to make the function look like this:
juce::Component* PlaylistComponent::refreshComponentForCell(int rowNumber,
int columnID,
bool isRowSelected,
juce::Component* existingComponentToUpdate)
{
if (columnID == 2)
{
if (existingComponentToUpdate == nullptr)
{
juce::TextButton* btn = new juce::TextButton{ "play" };
btn->addListener(this);
existingComponentToUpdate = btn;
}
}
return existingComponentToUpdate;
}