您需要在表格中添加按钮吗?这是一个例子
.h
class PlaylistComponent : public juce::Component,
public TableListBoxModel,
public Button::Listener
{
.....
Component *refreshComponentForCell(int rowNumber, int columnId, bool , Component *existingComponentToUpdate) override;
void paintCell(Graphics &, int rowNumber, int columnId, int width, int height, bool ) override;
void buttonClicked(Button *button) override;
void setTracks(Array<File> tracksFile);
TableListBox tableComponent;
.....
}
.cpp
PlaylistComponent::PlaylistComponent()
{
// In your constructor, you should add any child components, and
// initialise any special settings that your component needs.
tableComponent.getHeader().addColumn("", 1, 20);
tableComponent.getHeader().addColumn("Track Title", 3, 500);
tableComponent.setModel(this);
addAndMakeVisible(tableComponent);
}
Component * PlaylistComponent::refreshComponentForCell(int rowNumber, int columnId, bool , Component *existingComponentToUpdate)
{
if (columnId == ) // The ID and Length columns do not have a custom component
{
jassert(existingComponentToUpdate == nullptr);
return nullptr;
}
if(columnId == 1)
{
// Creating component if does not existst yet
TextButton * btn = static_cast<TextButton *> (existingComponentToUpdate);
if (btn == 0)
{
btn = new TextButton { "Play" };
}
String id{ std::to_string(rowNumber) };
btn->setComponentID(id);
btn->addListener(this);
existingComponentToUpdate = btn;
}
return existingComponentToUpdate;
}
void PlaylistComponent::paintCell(Graphics &g, int rowNumber, int columnId, int width, int height, bool )
{
if (columnId == 2) {
g.drawText(trackTitles[rowNumber], 2, 0, width - 4, height, Justification::centredLeft, true);
}
}
void PlaylistComponent::setTracks(Array<File> tracksFile)
{
for (int i = 0; i < tracksFile.size(); i++)
{
trackTitles.push_back(tracksFile[i].getFileName());
}
tableComponent.updateContent();
}
void PlaylistComponent::buttonClicked(Button *button)
{
//Array<File> file;
player->loadURL(file[std::stoi(button->getComponentID().toStdString())]); // your player
player->start();
}
那么在桌子附近应该有一个加载大量音频文件的按钮,你可以将它存储在一个向量中
Array<File> file;
..........
void loadAudioFiles()
{
juce::String filters = "*.mp3";
FileChooser chooser("Select an mp3 files..", File::getSpecialLocation(File::userHomeDirectory), filters);
if (chooser.browseForMultipleFilesToOpen())
{
file = chooser.getResults();
playlistComponent.setTracks(file );
}
}