Hi,
I was trying to create a tableListBox component that holds data from WAV files. So far it will show the box but when I press a button to select a file through the file chooser it adds the file to the array that I am using but then does not update the table. It updates the number of rows of the tableListBoxModel but not the tableListBox.
I used the tutorial to help with this but found it very confusing since it uses xml files and does not incorporate any kind of update function.
This is my tableListBoxModel:
**class** QueueTableModel : **public** TableListBoxModel,
**public** Component
{
**public** :
QueueTableModel();
~QueueTableModel();
**void** resized() **override** ;
**void** addNewItem(File* file);
**private** :
Array<QueueItem*> items;
**int** getNumRows() **override** ;
**void** paintRowBackground(Graphics& g, **int** rowNumber, **int** width, **int** height, **bool** rowIsSelected) **override** ;
**void** paintCell(Graphics &g, **int** rowNumber, **int** columnId, **int** width, **int** height, **bool** rowIsSelected) **override** ;
Component* refreshComponentForCell( **int** rowNumber, **int** columnId, **bool** isRowSelected, Component* existingComponentToUpdate) **override** ;
QueueTableHeader header;
TableListBox embeddedTable;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (QueueTableModel)
};
And this is the .cpp:
QueueTableModel::QueueTableModel()
{
addAndMakeVisible(embeddedTable);
embeddedTable.setHeader(&header);
embeddedTable.setModel(this);
items.clear();
}
QueueTableModel::~QueueTableModel()
{
for(int i = 0; i < items.size(); i++)
{
delete items[i];
}
}
void QueueTableModel::resized()
{
embeddedTable.setBounds(0, 0, getWidth(), getHeight());
}
int QueueTableModel::getNumRows()
{
return items.size();
}
void QueueTableModel::paintRowBackground(Graphics& g, int rowNumber, int width, int height, bool rowIsSelected)
{
if(rowIsSelected == true)
{
g.setColour(Colours::green);
}
else if(rowNumber % 2 == 0)
{
g.setColour(Colours::lightblue);
}
else if(rowNumber % 2 != 0)
{
g.setColour(Colours::orange);
}
g.fillAll();
}
void QueueTableModel::paintCell(Graphics &g, int rowNumber, int columnId, int width, int height, bool rowIsSelected)
{
String stringToDraw;
if(columnId == 1)
{
stringToDraw = String(items[rowNumber - 1]->getItemIndex());
}
else if(columnId == 2)
{
stringToDraw = items[rowNumber - 1]->getFileName();
}
else if(columnId == 3)
{
stringToDraw = String(items[rowNumber - 1]->getFileSize());
}
else if(columnId == 4)
{
stringToDraw = items[rowNumber - 1]->getLengthInTime();
}
g.drawText(stringToDraw, 0, 0, width, height, Justification::centred);
}
Component* QueueTableModel::refreshComponentForCell(int rowNumber, int columnId, bool isRowSelected, Component* existingComponentToUpdate)
{
if(columnId == 5)
{
return items[rowNumber - 1]->getPlayTimeLabel();
}
else if(columnId == 6)
{
return items[rowNumber - 1]->getStopTimeLabel();
}
else if(columnId == 7)
{
return items[rowNumber - 1]->getPlayButton();
}
else
{
return nullptr;
}
}
void QueueTableModel::addNewItem(File* file)
{
int currentNumFiles = items.size();
QueueItem* newItem = new QueueItem(currentNumFiles + 1, file);
items.add(newItem);
embeddedTable.updateContent();
}