Display array of files to Table

I want to make a table that displays files loaded from a directory. I have a button that is clicked to open a file chooser and when the file is chosen it is pushed into a vector and then drawn to a call in the table. I am able to compile and run the program, I click the button to load, a file chooser appears but when I choose a file nothing happens, so I’m missing something. This is my first time doing something like this, so if anyone can suggest what is missing from my code? The table is visible I can see the headers, would it be better to draw empty cells first to be populated? Do i need a method to update the table somewhere else? There are no error messages, the program just continues to run, so something to do with displaying the file name isn’t right.

void TableComponent::loadToTable(juce::URL someFile)
{
    for (auto i : fileNames)
    {
        fileNames.push_back(someFile.getFileName());

    }
    tableComponent.updateContent();
}

void TablesComponent::buttonClicked(Button* button)
{
    if (button == &loadButton)
    {
        juce::FileChooser chooser{ "Select a file.." };
        if (chooser.browseForFileToOpen())
        {
            loadToTable(juce::URL{ chooser.getResult() });
        }
    }

void TableComponent::paintCell(Graphics& g, int rowNumber, int columnId, int width, int height, bool rowIsSelected)
{
    if (columnId == 1)
    {
        g.drawText(fileNames[rowNumber],
            2, 0,
            width - 4, height,
            juce::Justification::centredLeft, true);
    }
}

You seem to be doing a redundant loop in the loadToTable method, it should probably just be the following :

void TableComponent::loadToTable(juce::URL someFile)
{
    fileNames.push_back(someFile.getFileName());
    tableComponent.updateContent();
}

With your loop, the push_back call would never be executed if there’s nothing already in the vector. And also, if something was there, it would also lead to the wrong behavior.

Secondly, have you implemented the virtual getNumRows method to return the number of elements (fileNames.size() ) in your vector?

Thirdly, you should probably check in the paintCell method that the row that is given as an argument will be within the limits of your vector, with something like :

if (columnId == 1 && rowNumber>=0 && rowNumber<fileNames.size())